Updated March 17, 2023
Overview of Inheritance in Java
Unlike other programming languages such as C, Java offers a mechanism by which another class’s data members and methods are inherited. This process is known as Inheritance in Java. In order to inherit from a class, the keyword ‘extends’ is used. Inheritance in Java permits the reusability of code so that a class only needs to write the unique features, and the rest of the code can be extended from the other class. The class that inherits from the other class is known as subclass or child class, and the class being inherited is known as parent class or superclass. Syntax, types, and implementation of inheritance will be discussed in the following sections.
Types of Inheritance in Java
Inheritance signifies an IS-A relationship which is otherwise known as a parent-child relationship. Parent and child are already mentioned in the introduction part.
Before moving to the types of Inheritance in java, first, let us see the syntax of Inheritance.
Syntax:
class apple extends fruit
{
//methods and fields
}
Here, apple is the subclass, and fruit is the parent class. This means Apple has certain unique properties, and it also has the properties of the fruit.
There are 4 different types of Inheritance in Java.
- Single Inheritance
- Multi-level Inheritance
- Hierarchical Inheritance
- Multiple Inheritance( With the help of interfaces)
Now, let us see each of them in detail.
1. Single Inheritance
A class that extends only one class. In the following example, class apple extends class fruit.
i.e. Fruit is the superclass, and Apple is the subclass that extends the properties and behavior of the Fruit class.
Following is the demonstration of single Inheritance in Java.
Code:
//Java program to demonstrate Single Inheritance
//parent class
class fruit {
public void taste()
{
System.out.println("Fruits are sweet");
}
}
//child class of fruit
class apple extends fruit
{
public void shape()
{
System.out.println("Apple is round");
}
}
public class InheritanceExample
{
public static void main(String[] args) {
apple fr = new apple(); //object of child class
fr.taste(); //call method of parent class
fr.shape(); //call method of child class
}
}
Output:
2. Multi-Level Inheritance
In this type of inheritance, a class will be extended from another class and the derived class act as the base class for some other class.
For example, in the figure, a class four-wheeler is the parent class, and the car is the derived class of the four-wheeler class. At the same time, the class car can be the base class for class Maruti.
Code:
//Java program to demonstrate Multiple Inheritance
//parent class
class fourwheeler {
public void wheels()
{
System.out.println("I have 4 wheels");
}
}
//child class of fourwheeler and parent of maruti
class car extends fourwheeler
{
public void type()
{
System.out.println("I'm a car");
}
}
//child class of car class
class maruti extends car
{
public void comp()
{
System.out.println("I'm maruti");
}
}
public class InheritanceExample
{
public static void main(String[] args) {
maruti fr = new maruti(); //object of child class
fr.wheels();
fr.type();
fr.comp();
}
}
Output:
3. Hierarchical Inheritance
In Hierarchical inheritance, a base class has more than one child class, which means different classes acquire the class’s properties.
For example, a class vehicle has subclasses cars, bikes, scooters.
Code:
//Java program to demonstrate Hierarchical Inheritance
//parent class
class vehicle {
public void wheels()
{
System.out.println("I have wheels");
}
}
//first child class of vehicle class
class bike extends vehicle
{
public void countwl()
{
System.out.println("I am a bike and has 2 wheels");
}
}
//second child class of vehicle class
class car extends vehicle
{
public void countwlc()
{
System.out.println("I am a car and has 4 wheels");
}
}
//third child class of vehicle class
class scooter extends vehicle
{
public void countwls()
{
System.out.println("I am a scooter and has 2 wheels");
}
}
public class InheritanceExample
{
public static void main(String[] args) {
scooter sc = new scooter(); //object of scooter class
sc.wheels();
sc.countwls();
car c = new car(); //object of car class
c.wheels();
c.countwlc();
bike b= new bike();//object of bike class
b.wheels();
b.countwl();
}
}
Output:
4. Multiple Inheritance
Multiple inheritances in java is a type of inheritance in which a class has more than one parent class.
For example, class C acquires the properties of both class A and class B.
But, in Java, Multiple Inheritance can be achieved only by using interfaces.
Suppose multiple inheritances are implemented like other types of inheritance, a compile-time error can occur as follows.
Code:
//Java program to demonstrate multiple Inheritance
//parent class
class A
{
public void hh()
{
System.out.println("A is working");
}
}
class B
{
public void hh()
{
System.out.println("B is working");
}
}
class C extends A,B
{
//code for class c
}
public class InheritanceExample
{
public static void main(String[] args) {
C sc = new C(); //object of C class
C.hh();
}
}
Here, the output won’t be displayed as it contains a compile-time error.
Importance of Inheritance in Java
- Code Reusability: Inheritance helps in reducing the rewriting of code. i.e. Code can be reused by other classes, and the child class only has to write its own unique properties. This reduces the time consumption and complexity of the code.
- For Method Overriding: The child class declares a method that is already present in the parent class, then it is known as method overriding.
Syntax:
final class A
{
. . .
}
Class B extends A
{
. . .
}
In this case, an error will be generated which says ‘Cannot inherit from the final A’.
Conclusion
It is a property by which another class acquires a class’s properties and behavior to provide the reusability of code. There are different types of inheritance in Java, such as Single Inheritance, Multi-level Inheritance, Hierarchical Inheritance, and Multiple Inheritance in Java. In this document, several important aspects of inheritance are covered.
Recommended Articles
This is a guide to Inheritance in Java. Here we discuss the importance and different types of inheritance in java along with syntax, examples, and code implementation. You may also look at the following articles to learn more-