In Object Oriented Programming scheme, Inheritance is all about Properties and Behaviour sharing between two or multiple Objects. When any class inherits all the properties and behavior of any class, called Derived class or Child class and whose properties and behaviors are inherited, called Parent class or Superclass.
Inheritance is a Mechanism where one Object acquires the properties and behaviour of other Object.
This represents IS-A relationship, also knoiwn as parent-child relationship.
This can be multiple types, are as follows:
1. Single :
When child class extending only one class and the parent class does not extend any other class, called Single Inheritance. This is completely supported by Java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class ClassA { void something(){ System.out.println("Hello World!"); } } class ClassB extends ClassA { } void main(String[] args) { ClassB classB = new ClassB(); classB.something(); } |
2. Multiple :
When child class extending more than one class then Its called Multiple Inheritance.
Java does not support Multiple inheritances. We can not extend more than one class in Java it will be a compile-time error.
The interface in Java, supports Multiple Inheritances. We can extends more than one Interface to child interface.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class ClassA { void something(){ System.out.println("Hello World!"); } } class ClassB { void something(){ System.out.println("Hello World!"); } } class ClassC extends ClassA,ClassB { // Compile time Error } |
3. Multi-level :
When Child class extends any other class which is already Inherits the property of any other class, called Multilevel inheritance occurred.
1 2 3 4 5 6 7 8 9 10 11 12 |
class ClassA { void something(){ System.out.println("Hello World!"); } } class ClassB extends ClassA { } class ClassC extends ClassB { } |
4. Hierarchical :
When two or more classes inherit one parent class, called hierarchical inheritance occurred.
1 2 3 4 5 6 7 8 9 10 11 12 |
class ClassA { void something(){ System.out.println("Hello World!"); } } class ClassB extends ClassA { } class ClassC extends ClassA { } |
5. Hybrid :
when any class extends multiple classes which are already the inheriting property of any other class, called Hybrid inheritance occurred.
This kind of inheritance is also not supported by Java or May depend on your cases.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class ClassA { void something(){ System.out.println("Hello World!"); } } class ClassB extends ClassA { } class ClassC extends ClassA { } class ClassD extends ClassB, ClassC { // Compile time Error } |
For More Details watch the video:
[embedyt] https://www.youtube.com/watch?v=FVSM6bDQ6gs[/embedyt]
Purpose to use:
- Code-Reuse: This mechanism whereby a subclass re-use code in a base class. By default, the subclass retains all of the operations of the base class, but the subclass may override some or all operations, replacing the base-class implementation with its own.
- Achieve Polymorphism
Issues:
Complex inheritance, or used within an insufficiently mature design may lead to the yo-yo problem.