Advertisement

What is Inheritance in Object Oriented Programming

 What is Inheritance?


It is a mechanism in which one class acquires (inherit) the property of another. For example, a child inherit some of merit or demerit from their parent. Object Oriented Programming highlight this to create flexibility for a programmer.


Example of Inheritance.


Let assume we have a vehicle class, which have attributes like, tires, clutch, sterling, break, accelerator and function (methods) like start, moving etc. Now we are creating another class name car so we have to declare these attributes and method (which we already declare in vehicle class) to this class also. This is called redundancy in software engineering. To overcome this issue Object oriented programming coin this word “Inheritance”. Now here we can inherit the properties of vehicle class in car class because every vehicle has break, sterling, clutch, tires and methods like start, moving. So we inherit all these properties form vehicle class, now car and truck are vehicle also but these two vehicle has some similar properties and also some different properties. Those properties which are similar, we can inherit these from vehicle class (we don’t have to write these again and again), and those characteristic which are different we can include these in that particular class i.e. a car has 4 tires and a truck has 10 or more tires.


Types of Inheritance


There are many type of inheritance such as Single Inheritance, Multiple Inheritance, hierarchical inheritance and hybrid inheritance.


Single Inheritance:


Here a class can inherit the attribute (properties) of only one class. The class which inherit the properties of other class is called subset of that class and other class is called super set of that class. In the below example the Class B inherit the properties of Class A.

 

Seven Testing Principles Software Testing

Multiple Inheritance:


Here a class can inherit the properties of more than one class. Let we have Class A and Class B now we declare another class Class C which require the properties of class A and B. So we Inherit Class A and Class B to Class C. This is called Multiple Inheritance.


Multilevel Inheritance:


In this type, one class can inherit from a derived class. Hence, the derived class becomes the base class for the new class.



In the above diagram Class C is subclass of B and B is subclass of Class A.

 


Post a Comment

0 Comments