Java Inheritance - The Coding Shala
Home >> Learn Java >> Java Inheritance
Java Inheritance is an important part of OOPs. Inheritance is a mechanism in which one class(object) acquires all the properties and behaviors (fields and methods) of another class(parent). When we create a class A, now we want to create another class and want to write the same method of class A so instead of writing the same methods again in the class we inherit methods of class A, this is called Inheritance. Using Inheritance we can reuse the code. Inheritance represents the IS-A relationship, also known as a parent-child relationship.
The following are important terms used in Inheritance:
- Child Class: Also known as a derived class, extended class or subclass. The child class is a class that inherits the other class.
- Parent Class: Also known as superclass or base class. A superclass is a class from where a subclass inherits the features.
- Reusability: As the name suggests, we can reuse the fields and methods of the existing class when we create a new class. Java Inheritance is also known for code reusability.
The syntax for Java Inheritance
We use the 'extends' keyword for inheritance. It means when we create a new class it inherits properties from the parent class. The meaning of 'extends' is to increase functionality.
Example of Java Inheritance
The following example explains the Java Inheritance:
Types of Inheritance
The following are different types of Java Inheritance:
- Single Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- Multiple Inheritance
- Hybrid Inheritance
Note: Java does not support Multiple and Hybrid Inheritance with classes. Multiple and Hybrid inheritance can only be achieved only through Interfaces.
Example of Single Inheritance in Java
The following example explains the single inheritance:
If a child class inherits the features of a base class and as well as child class also acts as a base class for another class is called multilevel inheritance. The below image explains multilevel inheritance:
Example of Multilevel Inheritance in Java
The following example explains the multilevel Inheritance:
Note: In Java, a class cannot directly access the grandparent's members. A class only can directly access it's parent's members. The following example explains how to access grandparent's member in Java:
In Hierarchical Inheritance, more than one subclasses inherit the features from a single parent class. The following image explains hierarchical inheritance:
Example of Hierarchical Inheritance
The example of Hierarchical Inheritance is as follows:Output:
Why multiple inheritances are not supported in Java?
Java does not support multiple inheritances to reduce the complexity and simplify the language. Multiple inheritances are only achieved by interfaces in Java. The following example explains why multiple inheritances are not supported in Java:
Important Points about Inheritance in Java
The following are some important points about inheritance:
- Every child class has one and only one direct superclass.
- A child class inherits all the members(fields, methods) from its superclass. Constructors are not members, so they are not inherited by subclasses, but they can be invoked from the subclass.
- A child class does not inherit the private members of its parent class.
- We can use inherited fields and methods in child class and also can write a new one also that is not in the superclass.
- During inheritance, the only object of child class is created not the superclass. The following example explains it:
Here in this example hash Code of superclass and subclass is the same that means only one object is created for the child class. Hash Code is a unique id number allocated to an Object by JVM.
Other Posts You May Like