Sitemap
ILLUMINATION’S MIRROR

Twin of ILLUMINATION to scale out and support more writers. Inquiries: https://digitalmehmet.com/ Subscribe to our content marketing strategy newsletter: https://drmehmetyildiz.substack.com/ External: https://illumination-curated.com

Complete Inheritance in Java with Examples

7 min readJun 25, 2026

--

In this tutorial, we are going to learn about what is inheritance in Java with its different types and how to implement each type with coding examples. Learning inheritance in Java is crucial because it is a very important concept of object-oriented programming(OOP). Interviewers often ask interview questions on the inheritance concept in Java to test your knowledge and ability to apply inheritance to solve real-world problems to develop efficient systems.

What is Inheritance in Java?

Inheritance is a core concept in Object-Oriented Programming (OOP) that allows one class to inherit the properties (fields and methods) from another class. This helps in code reusability and establishing a relationship between classes. In Java, we can achieve inheritance using the “extends” keyword.

Types of Inheritance in Java:

Basically, there are 5 types of inheritance. But, In Java using class we can achieve only three types of inheritance: single, multilevel , and hierarchical. We can achieve multiple and hybrid inheritance using interfaces.

In this post, we are going to cover all 5 types of inheritance in Java. So, let’s learn and understand what inheritance is, along with its definition and examples.

1. Single Inheritance

Single Inheritance, Single Inheritance examples, Single inheritance in java programming
Single Inheritance By CodeForHunger

If one class extends only one parent class then this is called as single inheritance. In the below example, we can clearly see that class B inherits class A, and using that class B we can easily access all data members present in class A. Here, A is a parent class of class B and B is a child class of A.

Example of Single Inheritance:

class A{
int speed = 300;
}

class B extends A{
void bike() {
System.out.println("Speed of the bike is: "+speed);
}
}

public class SingleIn {

public static void main(String[] args) {
//Creating the object of class B
B obj = new B();
obj.bike();
}
}

OUTPUT:

Speed of the bike is: 300

2. Multilevel Inheritance

Multilevel Inheritance, Multilevel Inheritance example, Multilevel inheritance in Java
Multilevel Inheritance By CodeForHunger

Multilevel inheritance in Java means that one class can inherit from another class that already inherited from a different class. It’s like building blocks, where each class inherits and extends the features of the classes above it. In the following example, we can see that the Child class inherits the class and the Father class inherits another class called GrandFather. So, this type of inheritance is called as multilevel inheritance.

Example of Multilevel Inheritance:

class GrandFather{
void love(){
System.out.println("Loving their Grandchild.... ");
}
}
class Father extends GrandFather{
void work(){
System.out.println("Working.... ");
}

}
class Child extends Father{
void play(){
System.out.println("Playing.... ");
}

}
public class MultilevelIn {

public static void main(String[] args) {
//using child class object we can happily access parent class methods
Child obj = new Child();
obj.play();
obj.work();
obj.love();
}

}

OUTPUT:

Playing….

Working….

Loving their Grandchild….

3. Hierarchical Inheritance

Hierarchical Inheritance, Hierarchical inheritance example , Hierarchical Inheritance in Java
Hierarchical Inheritance By CodeForHunger

When more than one class inherits a single class, then it is called as hierarchical inheritance. In the example below, we can clearly see that the Child_1 and Child_2 classes extend a single Parent class. Therefore, by using objects of both child classes, we can call the parent class method.

Get CodeForHunger’s stories in your inbox

Join Medium for free to get updates from this writer.

Example of Hierarchical Inheritance:

class Parent{
void property() {
System.out.println("Parents Property");
}
}
//Here child_1 and child_2 extends same parent class
class Child_1 extends Parent{
void c1_property() {
System.out.println("Child-1 Property");
}
}
class Child_2 extends Parent{
void c2_property() {
System.out.println("Child-2 Property");
}
}
public class HierarchicalIn{

public static void main(String[] args) {

Child_1 obj1 = new Child_1();
obj1.property();
obj1.c1_property();

Child_2 obj2 = new Child_2();
obj2.property();
obj2.c2_property();
}
}

OUTPUT:

Parents Property

Child-1 Property

Parents Property

Child-2 Property

These are examples of single, multilevel, and hierarchical inheritance in Java. However, as I mentioned, we can achieve hybrid and multiple inheritance in Java using interfaces only. Now, let’s explore what is hybrid and multiple inheritance in Java with some examples.

4. Multiple Inheritance

Multiple Inheritance, Multiple Inheritance with Example, Multiple Inheritance Program
Multiple Inheritance By CodeForHunger

Multiple inheritance in Java refers to a situation where a class can inherit properties and behaviors from more than one parent classes.

For example, let’s consider two parent interfaces: Coder and Dancer. The Coder interface contains a method called coding(), while the Dancer interface contains a method called dancing(). Now, if we have class called Person that implements the Coder and Dancer interfaces, then we are allowed to provide the implementation for the coding() and dancing() methods in the Person class, as shown in the below example..

Example of Multiple Inheritance:

interface Coder {
void coding();
}

interface Dancer {
void dancing();
}

class Person implements Coder, Dancer {
public void coding() {
System.out.println("Person is coding...");
}

public void dancing() {
System.out.println("Person is dancing...");
}
}

public class Main {
public static void main(String[] args) {
Person person = new Person();
person.coding();
person.dancing();

}
}

OUTPUT:

Person is coding…

Person is dancing…

5. Hybrid Inheritance

Hybrid Inheritance By CodeForHunger

In Java, hybrid inheritance refers to a combination of different types of inheritance, such as single inheritance and multiple inheritance. Single inheritance allows a class to inherit from a single superclass, whereas multiple inheritance enables a class to implement multiple interfaces, and the combination of both is often referred to as “hybrid inheritance” in Java.

Example of Hybrid Inheritance:

// Superclass
class Human {
void eat() {
System.out.println("Human is eating...");
}
}

// Interface 1
interface Coder {
void coding();
}

// Interface 2
interface Dancer {
void dancing();
}

// Subclass implementing single inheritance and multiple interfaces
class Person extends Human implements Coder, Dancer {
public void coding() {
System.out.println("Person is coding...");
}

public void dancing() {
System.out.println("Person is dancing...");
}
}

// Main Class
public class Main {
public static void main(String[] args) {
Person person = new Person();
person.eat(); // Inherited from Human
person.coding(); // Implemented from Coder interface
person.dancing(); // Implemented from Dancer interface
}
}

OUTPUT:

Human is eating…

Person is coding…

Person is dancing…

In the example above, the Person class inherits from the Human class using single inheritance. It also implements the Coder and Dancer interfaces using multiple inheritance of interfaces. The Person class overrides the eat() method inherited from Human and implements the coding() and dancing() methods from the Coder and Dancer interfaces, respectively.

That’s all about inheritance in Java, definition, and types with examples. If you like these simple examples of inheritance, then share them with your friends and If you have any doubts please discuss them below in the comment section…. :)

==> Related Interview Questions:

Q) Can a constructor be inherited in Java? Why?
No, constructors cannot be inherited in Java. A constructor belongs to its own class and is used to initialize the objects of that class. Since a constructor’s purpose is to create and initialize objects of a specific class, it cannot be inherited by a child class. However, a child class can call the parent class constructor using the super() keyword.

Q) Can a child class access all the members of a parent class?
No, a child class cannot access all the members of a parent class. It can access public and protected members directly. It can also access default members if both classes are in the same package. However, private members of the parent class cannot be accessed directly by the child class because they are restricted to the parent class only.

Q) What is the difference between IS-A and HAS-A relationship in Java?
=> An IS-A relationship represents inheritance. It means one class is a specialized or advanced form of another class. For example, a Dog is an Animal, so Dog can inherit from Animal.
=> A HAS-A relationship represents composition. It means one class contains an object of another class. For example, a Car has an Engine.

Q) Why is multiple inheritance not supported in Java using classes?
In Java, we can’t use multiple inheritance with classes because it can cause a problem called the “diamond problem.” This problem occurs when a class wants to inherit from two or more parent classes that have a method with the same name, it becomes confusing for the class to decide which method to use. To avoid this confusion and keep things simple, Java doesn’t allow multiple inheritance with classes.

Q) Why is ambiguity problem not there in the Java interface?
There is no ambiguity in interfaces in Java. This is because interfaces are traditionally declared without method implementations; they only define method signatures. The actual implementation of these methods is provided by the class that implements the interface.

Talking about default methods introduced in Java 8, if two interfaces contain the same default method, Java does not automatically resolve the conflict. Instead, the implementing class must override that method and explicitly choose which interface’s default implementation to use or provide its own implementation. This ensures that there is no ambiguity about which method implementation is used, because the implementing class resolves the conflict explicitly.

For more Java interview questions with simple answers get our complete Java interview preparation Kit, designed for both freshers and experienced developers. It covers Core Java, Advanced Java, Spring Framework, Spring Boot, Spring Data JPA, Spring Security, Microservices, and SQL with easy-to-understand explanations, practical examples, and interview-focused content.

--

CodeForHunger
CodeForHunger

Written by CodeForHunger

Here, I will share programming language tutorials and tech updates. Learn C and Java with tutorials, coding examples & interview questions. Follow for more...

ILLUMINATION’S MIRROR
ILLUMINATION’S MIRROR

Published in ILLUMINATION’S MIRROR

Twin of ILLUMINATION to scale out and support more writers. Inquiries: https://digitalmehmet.com/ Subscribe to our content marketing strategy newsletter: https://drmehmetyildiz.substack.com/ External: https://illumination-curated.com