- It is an important feature of Object Oriented Programming Paradigm, which helps in code reusability.
- Inheritance allows us to create a new class based on a class that has already been defined.
- A class whose properties are being acquired by some other class or a class that is inherited is called the super class or base class.
- The class that acquires the properties of some other class or the class that does the inheriting is called the subclass or derived class.
- Example: If 'Tree' is the class dealing with the general characteristics of a tree data structure, and if another class 'BinaryTree' has to be defined, and as this is also a kind of tree, this can acquire the general characteristics from the 'Tree' class and only the additional characteristics meant for a binary tree (such as number of child nodes should be <= 2) can be added to the BinaryTree' class. Thus it avoids defining 'BinaryTree' class from the scratch. Here, 'Tree' is the super class. 'BinaryTree' is the subclass.
- Thus, a subclass is one which inherits all of the members defined by the super class and adds its own, unique members.
Points to remember:
- The subclass object will always have a complete super class object within it — with all its data members and methods. If a base class member is not accessible in a derived class, then it is not an inherited member of the derived class. However, even the members that are not inherited, still form a part of a derived class object.
- The base class methods that are inherited in a derived class can access all the base class members, including those that are not inherited.
- To derive a class from outside the package containing the base class, the base class must be declared as public. If a class is not declared as public it cannot be reached directly from outside the package.
- All the rules pertaining to inheritance are applicable to static ie., a private static variable of a base class is not inherited in the derived class whereas a protected static variable is inherited in the derived class.
- Constructors in the base class are never inherited, regardless of their attributes. But they can be invoked from the sub ie, a child class cannot be instantiated by using the parent's parameterized constructor.
Types of Inheritance:
There are 5 types of inheritance, generally
1) Single Inheritance: A subclass inherits only from a single class.Here, class 'B' inherits the features form class 'A'.
2) Multilevel Inheritance: A subclass inherits from another subclass. Here, class 'C' is inherited from class 'B' which in turn is inherited from a class 'A'.
3) Hierarchical Inheritance: Two or more subclasses inherit from a single superclass. Classes 'A', 'B' and 'C' inherit the properties of class 'D'.
4) Multiple Inheritance: A subclass inherits more than 1 superclass. But this type of inheritance is not supported in Java. Still, the functionality of multiple inheritance can be achieved in Java using interfaces. Class 'C' is inherited from two parent structures 'A' and 'B'.
5) Hybrid Inheritance: It is a combination of 2 or more inheritances, stated so far (single, multilevel, hierarchical)