Inheritance
A programming technique that is used to reuse an existing class to build a new class is known as inheritance.The new class inherits all the behavior of the original class. The existing class that is re-used to create a new class is known as super class,base class,or parent class.The new class that inherits the properties and functions of an existing class is known as subclass,derived class or child class.The inheritance relationship between the classes of program is called a class hierarchy .
Inheritance is one of the most powerful feature of object-oriented-programming.The basic principle of inheritance is that each subclass shares common properties with the class from which it is derived.The child class inherits all capabilities of the parent class and can add its own capabilities.
Example
Suppose we have a class named Vehicle.The subclass of this class may share similar properties such as wheels and motors etc. Additionally, a subclass may have its own particular characteristics.For example,a subclass Bus may have seats for people but another subclass.Truck may have space to carry goods.
A class of animals can be derived in to sub classes like mammals,amphibians,insects,reptiles.A class of vehicles can be divided into cars,trucks,buses,and motorcycles.A class of shapes can be divided into the sub-classes lines,ellipse and boxes,etc.
The above figure shows that vehicle is parent class and bus,truck and motorcycle are three sub classes.The upward arrows indicate that the sub classes are derived from the parent vehicle class.
Super class and sub classes in inheritance |
The above figure shows that vehicle is parent class and bus,truck and motorcycle are three sub classes.The upward arrows indicate that the sub classes are derived from the parent vehicle class.
0 comments:
Post a Comment