A programming technique that is used to reuse an existing class to build a new class is known inheritance .The new class inheritance.The new class inherits all the all the behavior of the original class. The existing class that is reuse to create a new class is known as super class,base class.The existing class that is reused to create a new class is called super class.
Inheritance is one of the most powerful features of object oriented programming .The basic principle of inheritance is that sub each class share common properties with class class form which it is derived .The child class inherit all capability of th parent class and can add its own capability.
Example
Suppose we have a class name vehicle.The subclass of this may share similar properties such as wheels and motor etc.A subclass may have its own particular characteristics.For example, a subclass bus may have seas for people but another subclass Truck may have space to carry goods.
A class of animal can be dividing into subclass like mammals,amphibians,insect,reptiles.
Advantages of Inheritance
Categories of Inheritance
Protected Access Specifier
The private access specifier of a class are only accessible in the class in which they are declared.The public data member are accessible from any in the program.The protected access specifier is different from private and public access specifier.It is specially used inheritance.It allow a protected data member to be accessed from all derived classes but not form anywhere else in the program.It means that child class can access all protected member of its parent member.
Accessing Member of Parent Class
An important issue inheritance in the accessibility of base class member the object class from derived.It is known as accessibility .
Accessing Constructors of Parent Class
The object write class can access certain constructors of the parent class.It will use as appropriate constructor from parent class if no constructor is declared in derived class.
Function Overriding
The process of declaring member function in derived class with same name and same signature as in parent class is known as function overriding.
Function overriding allow the user to use same name to calling the member function of different class .When a member function is overridden int he derived class the object derived class cannot access the function of parent class However, the function parent class can access by using scope operator.
Types of Inheritance
A parent can be inherited using private,protected or private type inheritance.The type of inheritance define the access status of parent class member int he derived class .
Public Inheritance
In public inheritance ,the access status of parent class member in the derived class remain the same.The public member parent class become public member of derived class.The protected member of parent class become protected member of derived class .The private member of parent class become private member of derived class.
SYNTAX
class child_class : public parent_class
{
body of the class
}
Protected Inheritance
In public inheritance the access status of parent class members in the derived class is restricted.The public of parent class become protected member of derived class .The protected member of parent class becomes protected members of derived class.The private data member class become protected class.
SYNTAX
class child_class : protected parent_class
{
body of the class
}
Private Inheritance
In public inheritance the access status of parent class members in the derived class is restricted.The private ,protected and public members of parent class all become the private member of derived class
SYNTAX
class child_class : private parent_class
{
body of the class
}
Multilevel Inheritance with Parameters
The member function in multilevel inheritance can the value to the member function parent class .When a function is overridden,the member function in child class calls the member function in the parent class .It can also send parameters value to parent class function.
Multilevel Inheritance
A type of inheritance in which a derived class inherit multiple base classes is known as multiple inheritance..In multiple inheritance , the derived class combines the member of all base classes.
Constructors in Multilevel Inheritance
In multiple inheritance the constructors in base classes child classes are executed when object of derived class is created is created .The constructors may accept parameters or they can be without parameters.
Inheritance is one of the most powerful features of object oriented programming .The basic principle of inheritance is that sub each class share common properties with class class form which it is derived .The child class inherit all capability of th parent class and can add its own capability.
Example
Suppose we have a class name vehicle.The subclass of this may share similar properties such as wheels and motor etc.A subclass may have its own particular characteristics.For example, a subclass bus may have seas for people but another subclass Truck may have space to carry goods.
A class of animal can be dividing into subclass like mammals,amphibians,insect,reptiles.
Advantages of Inheritance
- Reuse Ability
- Save Time and Effort
- Increases Program Structure and Reuse Ability
Categories of Inheritance
- Single Inheritance
- Multiple Inheritance
Protected Access Specifier
The private access specifier of a class are only accessible in the class in which they are declared.The public data member are accessible from any in the program.The protected access specifier is different from private and public access specifier.It is specially used inheritance.It allow a protected data member to be accessed from all derived classes but not form anywhere else in the program.It means that child class can access all protected member of its parent member.
Accessing Member of Parent Class
An important issue inheritance in the accessibility of base class member the object class from derived.It is known as accessibility .
Accessing Constructors of Parent Class
The object write class can access certain constructors of the parent class.It will use as appropriate constructor from parent class if no constructor is declared in derived class.
Function Overriding
The process of declaring member function in derived class with same name and same signature as in parent class is known as function overriding.
Function overriding allow the user to use same name to calling the member function of different class .When a member function is overridden int he derived class the object derived class cannot access the function of parent class However, the function parent class can access by using scope operator.
Types of Inheritance
A parent can be inherited using private,protected or private type inheritance.The type of inheritance define the access status of parent class member int he derived class .
Public Inheritance
In public inheritance ,the access status of parent class member in the derived class remain the same.The public member parent class become public member of derived class.The protected member of parent class become protected member of derived class .The private member of parent class become private member of derived class.
SYNTAX
class child_class : public parent_class
{
body of the class
}
Protected Inheritance
In public inheritance the access status of parent class members in the derived class is restricted.The public of parent class become protected member of derived class .The protected member of parent class becomes protected members of derived class.The private data member class become protected class.
SYNTAX
class child_class : protected parent_class
{
body of the class
}
Private Inheritance
In public inheritance the access status of parent class members in the derived class is restricted.The private ,protected and public members of parent class all become the private member of derived class
SYNTAX
class child_class : private parent_class
{
body of the class
}
Multilevel Inheritance with Parameters
The member function in multilevel inheritance can the value to the member function parent class .When a function is overridden,the member function in child class calls the member function in the parent class .It can also send parameters value to parent class function.
Multilevel Inheritance
A type of inheritance in which a derived class inherit multiple base classes is known as multiple inheritance..In multiple inheritance , the derived class combines the member of all base classes.
Constructors in Multilevel Inheritance
In multiple inheritance the constructors in base classes child classes are executed when object of derived class is created is created .The constructors may accept parameters or they can be without parameters.