Method Overriding and hiding

This chapter delves into the world of method overriding and hiding in C++, essential concepts for understanding inheritance and polymorphism. We'll explore the differences between these concepts, how to achieve them, their benefits, and when to use each approach effectively in your C++ code.

Inheritance Recap

Before diving into method overriding and hiding, let’s revisit inheritance, the foundation for these concepts. Inheritance allows you to create new classes (derived classes) that inherit properties (attributes) and behaviors (methods) from existing classes (base classes).

				
					class Animal {
public:
  void makeSound() {
    std::cout << "Generic animal sound..." << std::endl;
  }
};

class Dog : public Animal {
  // ... other members
};

				
			

Here, Dog inherits from Animal, gaining access to the makeSound() method.

What is Method Overriding?

Method overriding allows a derived class to redefine the behavior of a method inherited from the base class. This is particularly useful when the inherited behavior needs to be specialized for the derived class.

How to Override Methods

  • The derived class method must have the same name, parameter list (including types and order), and return type as the base class method.
  • Use the override keyword (introduced in C++11) to explicitly declare that you’re overriding a base class method. This improves code clarity and helps the compiler detect potential errors.
				
					class Dog : public Animal {
public:
  void makeSound() override {
    std::cout << "Woof!" << std::endl;
  }
};

				
			

In this example, the Dog class overrides the makeSound() method to provide a barking sound specific to dogs.

Benefits of Method Overriding

  • Polymorphism: Overriding enables polymorphism, where the same method call on a base class pointer or reference can invoke different implementations based on the actual derived class object being pointed to.
  • Code Specialization: Derived classes can adapt inherited behavior to their specific needs.

Code Example with Output:

				
					#include <iostream>

class Animal {
public:
  virtual void makeSound() {
    std::cout << "Generic animal sound..." << std::endl;
  }
};

class Dog : public Animal {
public:
  void makeSound() override {
    std::cout << "Woof!" << std::endl;
  }
};

int main() {
  Animal* animalPtr = new Dog; // Base class pointer to derived class object
  animalPtr->makeSound(); // Output: Woof! (Polymorphism in action)
  delete animalPtr;
  return 0;
}

				
			
				
					// output //
Woof!

				
			

In this code, we have a base class Animal with a virtual method makeSound(), and a derived class Dog that overrides this method using the override keyword.

In the main() function, we create a pointer animalPtr of type Animal* pointing to a dynamically allocated Dog object. When we call animalPtr->makeSound(), it invokes the makeSound() method of the Dog class (the derived class) due to polymorphism, printing “Woof!”.

It’s important to note that the makeSound() method in the base class is declared as virtual, allowing dynamic binding to occur based on the actual type of the object being pointed to by the base class pointer. This is why the makeSound() method of the Dog class is invoked even though the pointer is of type Animal*.

What is Method Hiding?

Method hiding, sometimes referred to as shadowing, occurs when a derived class has a method with the same name as a method in the base class, but the signatures (parameter list or return type) might differ. In this case, the derived class method hides the base class method, and calls to the method name will only access the derived class version.

How to Hide Methods

There’s no explicit keyword for hiding. Simply define a method in the derived class with the same name but a different signature compared to the base class method.

				
					#include <iostream>

class Animal {
public:
  void makeSound(int volume) { // Takes an integer argument
    std::cout << "Animal sound with volume: " << volume << std::endl;
  }
};

class Dog : public Animal {
public:
  void makeSound() { // No arguments, different signature
    std::cout << "Woof!" << std::endl;
  }
};

int main() {
  Dog dog;
  dog.makeSound(); // Calls the makeSound() method of the Dog class
  dog.makeSound(2); // Calls the makeSound(int) method of the Animal class
  return 0;
}

				
			
				
					// output //
Woof!
Animal sound with volume: 2

				
			

In this code, we have a base class Animal with a method makeSound(int volume) that takes an integer argument, and a derived class Dog that overrides the makeSound() method with a different signature (no arguments).

In the main() function, we create an instance of Dog named dog. When we call dog.makeSound(), it calls the makeSound() method defined in the Dog class, which prints “Woof!”. When we call dog.makeSound(2), it calls the makeSound(int volume) method inherited from the Animal class, which prints “Animal sound with volume: 2”. This demonstrates method overriding and how method signatures affect which method is called.

When to Use Method Hiding (Use with Caution)

Hiding methods can be useful in limited situations, but it’s generally less preferred than overriding due to potential confusion. Use it cautiously and with clear documentation to avoid unintended consequences.

Important Note: Hiding methods can make code harder to understand and maintain, especially for those unfamiliar with the class hierarchy. It’s often recommended to favor overriding for clear and predictable behavior.

Key Differences and Choosing the Right Approach

FeatureMethod OverridingMethod Hiding
SignatureSame name, parameter list, and return typeSame name, but different parameter list or return type
KeywordCan use override for clarity (C++11+)No specific keyword
BehaviorRedefines inherited behavior for the derived classHides the base class method with the same name
PolymorphismEnables polymorphismDoes not directly enable polymorphism
Use CaseWhen a derived class needs specialized behaviorUse with caution, for specific scenarios only

Choosing the Right Approach

  • Overriding: Prefer overriding when the derived class needs to provide a custom implementation for an inherited method while maintaining the same basic functionality. This promotes polymorphism and code clarity.
  • Hiding: Consider hiding only in rare cases where you intentionally want to prevent access to the base class method from the derived class, and you have clear documentation to explain the reasoning.

Advanced Considerations

Virtual Functions and Overriding

  • Virtual functions (declared with virtual in the base class) are crucial for proper method overriding. They ensure that the correct overridden method is called at runtime based on the actual object type (dynamic binding).
  • Without virtual functions, the base class method would always be called regardless of the derived class object.

Accessing Hidden Base Class Methods

In rare cases, you might need to access a hidden base class method from the derived class. You can achieve this using the scope resolution operator (::) with the base class name.

				
					class Animal {
public:
  void makeSound(int volume) {
    std::cout << "Animal sound with volume: " << volume << std::endl;
  }
};

class Dog : public Animal {
public:
  void makeSound() { // Hides base class makeSound(int)
    std::cout << "Woof!" << std::endl;
  }

  void useBaseClassSound(int volume) {
    Animal::makeSound(volume); // Accessing hidden base class method
  }
};

				
			

Potential Issues with Method Hiding

  • Method hiding can lead to confusion, especially for those unfamiliar with the class hierarchy. It can make code harder to understand and maintain.
  • If a base class method is hidden unintentionally, it can break existing code that relies on that method.

Method overriding and hiding are essential concepts in C++ object-oriented programming. Understanding their differences, benefits, and appropriate use cases is crucial for writing well-structured and maintainable code.Happy coding !❤️

Table of Contents

Contact here

Copyright © 2025 Diginode

Made with ❤️ in India