- It is one of the most useful, essential, fundamental & prime concepts of the object-oriented paradigm.
- Inheritance is the process by which one or many new classes (called derived/sub/ extended/child/descendant classes) are created from existing classes (called base/super/parent /ancestor /master classes).
- In another words, Inheritance is the procedure of creating a new class from one or more existing (old) classes is termed as inheritance.
- Inheritance is a technique of organizing information in a hierarchical form.
- It is a relation between classes that allows for definition and implementation of new class based on the definition of existing classes.
- In Inheritance, a class can be derived from more than one class and it is also possible to derive a class from previously derived class.
- Inheritance is represented as “is-a” relationship.
- Here, the existing classes are main components of inheritance.
- In inheritance, the main properties of existing classes are simply extended to the new classes.
- In Inheritance, the object of derived class can access members of base as well as derived class. On the other hand, the object of base class can not access members of derived classes.
The base classes –
Do not know about their sub classes.
- Super class is the class from where a subclass inherits the features.
- The derived classes –
- Subclass is a class which inherits the other class.
- may inherit some/none/all the features (capabilities) of the base class and can also add new specific features to the newly created derived class as per requirement which is decided by the use of visibility mode(public/private/protected).The base class remains unchanged.
- A derived class can also inherit properties from more than one class or from more than one level. A derived class has its own set of member variables and functions.
- The subclass can reuse the data member and methods of the super class that were already implemented and it can also extend or replace the behaviour in the super class by overriding methods.
- The super Keyword –
- The keyword ‘super’ refers to the super/base class of the current object.
- It performs a similar function, but it refers to the immediate super class of the current class.
- This is useful when a class needs to access: (1) its super classes constructor, and (2) its super classes methods.
- There are two uses of super keyword.
1. It is used for calling super class constructor.
2. It is used to access those members of super class that are hidden by the member of subclass .
- While sub classes inherit the attributes and methods of their super class, they do not inherit their super class’s constructors. To invoke a super class’s constructor, a sub class must use the super keyword as –
(Here, the argument list provides the arguments required by the super class’s constructor and may be empty for the default constructor. It is advise that call to the super class’s constructor must be the first statement in a sub class’s constructor.)
- Super class constructors are not inherited by sub class.
- We can create an instance of subclass and then assign it to super class variable, this is called upcasting.
- When an instance of Superclass is assigned to a Subclass variable, then it’s called downcasting. We need to explicitly cast this to Subclass.
class DerivedClassName : [Access Specifier] BaseClassName
// member of derived class
Access Specifier of Class Members in Inheritance
- There are three types of visibility modes/modifier used in inheritance. They are private, public and protected.
Base class visibility modes Derived class visibility modes Private derivation Public derivation Protected derivation private Not inherited private Not inherited Not inherited public private public protected protected protected protected
Types of Inheritance
(A) The inheritance is classified into following types on the basis of its derivation process from base class –
- When a class is derived/inherits the features from only one base class, it is called Single Inheritance.
- A derived class can be declared only if its base class is already declared.
- When a class is derived/inherits the features from more than one parent class (base classes), it is called multiple inheritance.
- When a class is derived/inherits the features from another derived class, is called Multi-level Inheritance.
- Derivation of several classes from a single base class is called hierarchical inheritance.
- Derivation of a class involving more than one form of inheritance is called hybrid inheritance.
- Derivation of a class from other derived classes, which are derived from the same base class is called multi-path inheritance.
(B)The inheritance is further classified into following types on the basis of use of base class members by derived class –
A class can be derived publicly or privately.No third type exists.
- When public access specifier is used in inheritance of derived class then the public members of the base class are public members of the derived class.Similarly protected and private members of the base class are protected and private members of the derived class.
- When a class is derived publicly, all the public members of base class can be accessed directly (by the object of derived class) in the derived class and all the private members of the base class can be accessed (by the object of derived class) using only public member functions of the base class.
- Syntax = class B : public A
- It is by default derivation inheritance.
- When private access specifier is used in inheritance of derived class then the public and protected/private members of the base class are private members of the derived class.
- When a class is derived privately/protected, an object of derived class has no permission to access even public members of the base class directly. In such a case, the public members of the base class can be accessed using public member functions of the derived class and private members of the base class can be accessed using public member function of base class via public member function of derived class.
- Syntax = class B : A or Syntax = class B : private A or Syntax = class B : protected A.
Benefits/Advantages of Inheritance
- The most important feature of using inheritance is for deriving a new class using existing class which provides re-usability concept. Here, the existing class remains unchanged.
- When inherited from another class, the derived class does not need to be rewritten.
- Code sharing easily occurs at several levels.
- Multiple inheritance provides sufficient guarantee that the behavior they inherit will be same in all cases.
- The derived class extend the properties of base classes to generate more dominant object.
- The same base class can be used to transfer similar properties into multiple derived class.
- The multiple base class can be used to transfer their properties into a single class to make it more usable/powerful.
- It allows the programmer to utilize the previously defined classes with newer ones.
Demerits/Disadvantages of Inheritance
- Inappropriate use of inheritance makes a program more complicated.
- Calling of member functions by an object produces more compiler overheads.
- In class hierarchy, multiple data elements remain unused for long time.
Use of Inheritance
- The concept of Inheritance is used in reusability , i.e. reuse of properties of base class in the derived class without rewriting again. Reusability permits to reuse the members of base class. Reusability is achieved using inheritance and both are not different from each other rather the outcome of inheritance is reusability. By reusability, the development time of software is reduced.
- Inheritance helps to organize software components into categories and subcategories resulting in classification of software.
- There are many benefits of inheritance i.e. they help in code reuse, ease of code maintenance and extension, and reduction in the development time.
Inheritance with Constructor & Destructor
548 total views, 1 views today