Overriding in Object Oriented Programming - an article by Birendra Keshari

CoderSource.net
Overriding in Object Oriented Programming - an article by Birendra Keshari
Rating:

Date: 5/1/2004 12:00:00 AM

   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.
 

You Can Rate this Article, if you are Logged In      
 

More Links from CoderSource.net:

 
Refer to a Friend:

Your Details:

Name:     e-mail:

Friend Details:

Name:    e-mail:    


MENU
Home
MFC 
C++
.Net
WIN32
Programming
Forum
My Articles
Add to Google
Add to My Yahoo!
Welcome to Codersource.Net Login | Register | Faq  

SEARCH
Google
 

NOTES:


Thanks for visiting our CoderSource.net. This site will be improved with more articles. Interested visitors can also submit their articles through the Submit Article link.Your article will also be published after due consideration by the editor. 

© Copyright 2003. All rights on content reserved by CoderSource.net. Contact    About Us