This is a common interview question asked and very important fundamental to understand to visualize how Object Oriented programming works. This feature of OOPS have vast implementation and is used in many design patterns.
First we start with the topic it is expected that you have some basic idea of Inheritance.
What does method overriding means: "Implementing a base class function in derived (child) class is methode overriding" .Basically you if there is a fucntion testFun() in a parent class, and same name function testFun() is also declared in derived class, in this case the child class overrides testFunc behavior.
Overriding Essential requirement:
Overriding Essential requirement:
- Inheritance is compulsive for method overriding.
- Overriding function should have same declaration in both base and derived class including exact same name, same return type and same parameter list.
class Base { public: void testFunc() { cout << "Base class"; } }; class Derived:public Base { public: void testFunc() { cout << "Derived Class"; } }int main() { Base b; //Base class object Derived d; //Derived class object b.testFunc(); //Early Binding Ocuurs d.testFunc(); }Output : Base class Derived class
What happened here ! Why derived class object is not seeing two declaration of testFunc(). One in base class and other in derived class. Why there are no linking error.
This is because of Early Binding, methods, functions and other properties which are detected and checked during compile time and all the function call are decided at compile time by an object. Linker needs not worry about any function call. The advantage of using early binding is for code performance and ease of development.
Where do we need to use this method overriding:
This is quire interesting that you might have understood the what function overriding means but it is more insetting to know why it is even there and so popular.
First have look at late binding also know as dynamic binding which is only possible with overridden methods
class Base { public: void show() { cout << "Base class"; } }; class Derived:public Base { public: void show() { cout << "Derived Class"; } } int main() { Derived d; //Derived class object Base* b = &d; //Base class pointer to derived class object b->show(); //Late Binding will Occur }In above program which show method will execute ?
Output: Derived Class
Interesting right! that how a base class pointer is invoking child class function. Actually this is the concept that you must understand well and use your logic to fit this in your program to make it more intelligent.
Analyzing function overriding practically:
To understand overriding practical use, think of mouse operation you use daily with your computer. All you do with this is clicks and windows Handlers do the work for you. How the window handler is doing this, its simple , window handler is a base class pointer which can point to any type of event class(will be all child). So depending upon user request necessary function of child handler is invoked with simple base class pointer control.
How it works internally from compiler to linker would be a long discussion to start here, rather you can explore that in this blog(Navigation)
Explore more about Method overriding in this website:
How does method overriding works in c++
How does method overriding helps in design patterns
No comments:
Post a Comment