C++ Programming: Method Overriding Vs. Method Hiding

Overriding and Hiding are two different concepts in method inheriting and implementation. However, many people confuse the two due to their similarities. Here I will try to clear the differences between the two concepts once and for all.

 

First some background for both concepts: We are talking about an inheriting condition where the Base class has some methods declared and implemented. Now we want to create methods with similar names in the Derived class.

Overriding

In this case the Derived class creates its own method that has the exact same signature as the Base class method. It has the exact same name, returned

value, arguments (parameters list), const / non-const declaration, etc.

In this case it is obvious that the Derived class has a different implementation to the exact same method, so when we are working with a Derived object we will prefer the Derived implementation (many time those methods will also be declared as virtual methods in the Base class).

Hiding

In this case the Derived class creates its own method that has the same name as the Base class method. Yet, its signature is different! I.e. the return value is different, or the arguments, or both, etc.

In this case we assume that since the

Derived class used the exact same name (and name means meaning); it actually wanted to give a different implementation to the Base class method (just like in Overriding). Yet, it could not use Overriding since in it more specific implementation it needed to change the method signature.

Therefore, we understand that despite the fact that the methods have different signatures they mean similar things and when working with a Derived object we would like to activate the Derived version of the method. Therefore, trying to activate the Base version of the method without explicitly stating it is the Base version (by using Base:: ) will end up with a compilation error.
 

You might also be interested in the following articles:

- C++ Programming: Void* vs. Templates

- Software Quality, or How do you Know a Software is Good!



Article Written By Ixodoi

Last updated on 29-07-2016 2K 0

Please login to comment on this post.
There are no comments yet.
How To Gain Karma Points Quickly On Reddit
Objects Oriented Programming: Oop 4 Major Principles