"In object-oriented programming (OOP), Inheritance is a way to compartmentalize and reuse code by creating collections of attributes and behaviors called objects which can be based on previously created objects." - Wikipedia
1.) Code re-usability: Using the code written somewhere in program.
Until C, Code re-usability can be achieved using making it into an function. Then the function would be called whenever it is needed. But in OOP, function is wrapped up into class. Required code cannot simply be called. Hence came the concept of Inheritance. Whenever any code stored in other class is needed, we make child class and child class automatically has the required code via - Inheritance.
To beat Code re-usability many point that required code can simply be "Copied" and "Pasted". This certainly solves the need but leads to Wiki-Long and repetitive code. What if later on repeated code is incorrect? Even "Search and Replace" feature would not help.
2) Maintainability - Extendability
Using Inheritance makes the code structured. Father - Child - Child and so on. Things are defined at appropriate level class. Thus the code looks neat. This means real easy debugging, easy to modify and easy to add new features.
3) Reduction in complexity
Now thats what everyone wants to hear. Programming is too complex!! Inheritance can surely lower the bar. Take an example.
You need to plan a trip. Now Lets review the idea to trip process.
- Think of a place
- Approach the people and talk about plan. Check who wants to come
- Everyone sits together and decides of mode of transport
- Meanwhile the others figure out route
- Finally exact time of departure is decided
Analyzing the above process,
- Initially the abstract idea is decided
- Once it is finalized, More details are added at later stage. We cannot directly build a complete detailed class.
- Task is divided into parts. We have different classes to do different jobs.
Thus as it is said that OOP was developed to handle the increasing complexity, it is fairly correct to assume that inheritance was backbone of it.
Enough with the flattery of Inheritance, There are also disadvantages of Inheritance. Can you think of one?
Enough with the flattery of Inheritance, There are also disadvantages of Inheritance. Can you think of one?
this is a good concept of polymorphism n inheritance
ReplyDelete