Inheritance is an important part of Object-Oriented Programming(OOP). Many beginners find it difficult to understand inheritance in java. Therefore, Letstacle team is here to provide Java help.

What is inheritance?
Derive the quality or characteristics from one’s parents.

What is Inheritance in Java?

Inheritance in java is the mechanism by which, one class acquire all the properties and behaviour of another class. Inheritance establishes an IS-A relationship(Parent-child relationship).

Why use Inheritance?

The two main objectives to use inheritance in Java programming or any other OOP’s language is-

  • To achieve method overriding.
  • And code reusability.

Firstly, let us understand some terminologies.

Super class/ Parent class- Class which is inherited.
sub class- Class that inherits super class.

In Java inheritance, a super class is inherited by a subclass.

Thus, a subclass can use the methods and variables declared in the super class.

In addition, we can modify the methods of the super class in the subclass (method overriding).

Syntax

class SubClass extends SuperClass{
//class body
}

The extends keyword indicates that you are making a new class that derives from an existing class. The meaning of “extends” is to increase the functionality.

Example of  Inheritance


//Birds class(parent class)
public class Birds {
	int eyes;
	int legs;
	void setValue(int eyes,int legs)
	{
		this.eyes=eyes;
		this.legs=legs;
	}

}
//Owl class(subclass)
public class Owl extends Birds {

	public static void main(String[] args)

	{
		Owl obj = new Owl();
		obj.setValue(2, 2);
		int eyes = obj.eyes;
		int legs = obj.legs;
		System.out.println("Owl has " + eyes + " eyes");
		System.out.println("Owl has " + legs + " legs");
	}
}


Example of Java inheritance

Java program output

In the above example, you can see how the object of the Owl class acquires all the properties and behaviours of a parent class.

Object obj of Owl class can access the methods and fields of Birds class using dot(.) operator.

Types of Inheritance in Java

  • Single
  • Multilevel
  • Hierarchical

Note: Multiple and Hybrid inheritance are not possible in java.
The reason behind this is explained at the end of this article.

However, if you are familiar with C++, then C++ support all types of inheritance.

Firstly, we will see examples of all sorts of inheritance possible in Java programming language.

Single Inheritance

When one class inherits another class it is single inheritance. In the Java code below, the Dog class inherits the Animal class.


//class animal is the parent class
public class Animal {
	void eat()
	{
		System.out.println("Animals can eat");
	}

}
//class Dog is the subclass of Animal
public class Dog extends Animal{
	void bark()
	{
		System.out.println("Dog barks");
	}
}
//Inheritance1 is the class with the main method
public class Inheritance1 {

	public static void main(String[] args) {
		Dog obj=new Dog();
		obj.eat();
		obj.bark();

	}

}

Output


Animals can eat
Dog barks

Java Multilevel Inheritance

When there is a chain of inheritance, it is known as multilevel inheritance. In the Java code below, Dog class inherits Animal and class Puppy inherits class Dog. So there is a chain of inheritance.


//class Animal is the parent class
public class Animal {
	void eat()
	{
		System.out.println("Animals can eat");
	}

}
//class Dog is the subclass of Animal
public class Dog extends Animal{
	void bark()
	{
		System.out.println("Dog barks");
	}
}
//Puppy is the subclass of class dog which is a subclass of class Animal
public class Puppy extends Dog{
	void weep()
	{
		System.out.println("Puppy weeps");
	}

}
//Inheritance2 is the class with the main method
public class Inheritance2 {

	public static void main(String[] args) {
		Puppy obj=new Puppy();
		obj.eat();
		obj.bark();
		obj.weep();

	}

}

Output

multilevel inheritance Java

multilevel inheritance Java output

Hierarchical Inheritance

Two or more classes inheriting a single class is known as hierarchical inheritance.

In the code below, as the Dog and Cat class extends Animal class there is hierarchical inheritance. The object of both classes can access methods of Animal class.

Cat class objects can not extend Dog class methods or fields.


//Animal class is the super class
public class Animal {
	void eat()
	{
		System.out.println("Animals can eat");
	}

}
//Dog is the subclass of animal
public class Dog extends Animal{
	void bark()
	{
		System.out.println("Dog barks");
	}
}
//Cat is the subclass of animal
public class Cat extends Animal {
	void meow()
	{
		System.out.println("Cat meows");
	}

}
//class containing the main method
public class Inheritance3 {

	public static void main(String[] args) {
		Cat obj=new Cat();
		obj.eat();
		//obj.bark(); will show compile error because Cat do not extend Dog
		obj.meow();

	}

}

Output

Hierarchical Inheritance code output

Hierarchical Inheritance output

Let us move to our pending topic.

Why multiple inheritance is not supported in Java?

Answer: Imagine there are two classes, A and B containing the same method show(). Let class C extend classes A and B.

For instance, now if we call show() method from child class object then there will be a compile error.

This type of error is called ambiguity error. Here, the compiler fails to understand which class show() method it will call.

Code Example


public class A {
	void show()
	{
		System.out.println("In A");
	}

}
public class B {
	void show()
	{
		System.out.println("In B");
	}

}
public class C extends A,B{//if possible

	public static void main(String[] args) {
		C obj=new C();
		obj.show();// now compiler fails to understand which class show() method to call

	}

}

Output
This code produces a compilation error.

In conclusion, as you saw multiple inheritance is not possible in Java. Thus, Hybrid inheritance is also not possible because Hybrid inheritance includes multiple inheritance.

✌️ Like this article? Follow us on Facebook and LinkedIn. You can also subscribe to our weekly Feed