Wednesday, 11 January 2012

Inheritance


What is common between a father and a son? At least one thing would be common – their assets!
  In real life, inheritance allows us to reuse things that belong to a particular entity. Also, in object oriented world a class can inherit the properties and functionalities defined in some another class so that they can be reused. Then we have to be a bit careful in designing these classes because reusability cannot be done unless
the classes are of the same type. So the class which would be reusing the functionalities of the other class in object oriented terms we would say that the class is “deriving” from the former class and is termed as the derived class. The class that is being “derived from” is termed as the base class. Inheritance directly results in the following benefits: --
Reusability: -- Inheritance results in functionalities defined in one class being reused in the derived classes. So the efforts of rewriting the same functionality for every derived class is being saved. This definitely saves a lot of development time.
Enhancement and Specification: -- Due to the characteristic of inheritance, we can club the common functionalities in the base class and have the specific functionalities in the derived class. This feature can be used to have a functionality defined in the base class to be further modified for betterment or specification by the derived class. This mechanism of redefining the functionality of the base class in the derived class is termed as “overriding”
Avoiding type inspection:-- In the case study that we discussed to understand procedural approach we had a strict type inspection routine at the library end wherein in every function in the library we had to check for the type of the employee for whom the work has to be done. With inheritance, we would have a common base class called as “Employee” which would have all the common functionalities defined where as the specific routines do be done for various types of employees would go in the respective derived classes. So there would be class defined for every type of employee and the class would all the specifications for that type of employee and would be derived from the base class Employee to inherit the common functionalities. Therefore, in the functions now we wont have to check for the type of employee every time because every
employee type has its own specific routines defined within it.