Overriding in Object Oriented Programming – an article by Birendra Keshari

   Overriding is an important concept in Object Oriented Technology. In today’s world you can’t live without overriding. For example in most of the API’s used for building windows based application you always need to override some parent window. Like in Java you may override methods and data of class Frame, in C++ CMainWnd, in QT Form etc. In many of your applications that you build using Inheritance you need to override some members of the parent class.

   Let’s understand inheritance by an analogous example. Let S be the son of F. There can be many characteristics that C inherit from his/her father F. Like may be he also loves listening to music and so does C, F is genius and so is C, F writes poems like C does. These all are inherited characteristics. But there can be situation like C writes romantic poems and his/her father used to write tragic poems. Here, C has overridden his/her father’s characteristics of writing a poem. It was inherited from his/her father but he/she does it differently. The concept of overriding is same in Object Oriented Programming Languages.

   For overriding, there must be inheritance. Once you inherit, you can then override data member of methods of the parent class. In C++ “:” is used to tell C++ compiler that you want to inherit something. For example “Class MangoTree : public Tree ” means class MangoTree is inheriting class Tree.
The keyword public adds the information that only the public members are inherited. Once class MangoTree inherits class Tree, all the public members of class Tree are accessible from class MangoTree. Like is there is a data member “height” defined in class “Tree” you can directly use
this variable in any method of class “MangoTree”. Similar is the case with methods. But, sometimes you would like to change the implementation of some of the methods of the parent class. Like though there was a method called, “getFruitName()”, you would write another method of the same name and parameter in class “MangoTree”. The method “getFruitName()” of parent class is not visible in child class “MangoTree” now. It has its own now. It has overridden the method of its parent. Similarly, it can also override the data member of its parent. This is what known as overriding, a popular technique in object oriented paradigm.

   The following code shows an example of inheritance while building a MFC application.


class Application: public CWinApp
{
BOOL InitInstance();
}

   In the above code fragment, class Application inherits class CWinApp. It is the case for every application that are build using MFC. Class Application also inherits, the method “InitInstance ” of class CWinApp. But, it overrides it. So, it can have its own new implementation. This method can be called from the CWinApp or some other class collaborated with CWinApp, thus, there is no need of knowing in advance what will be inside this method. The user can do the things inside this method in
his/her own way.