Open In App

Interfaces and Inheritance in Java

Last Updated : 30 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Java supports inheritance and interfaces, which are important concepts for building reusable code. A class can extend another class and can implement one and more than one Java interface.

Note: This topic has a major influence on the concept of Java and Multiple Inheritance.

Interface Implementation by Classes

In Java, a class can implement one or more interfaces with the help of the implements keyword.

Example: Implementing Multiple Interfaces

Java
//Driver Code Starts
// A class can implement multiple interfaces
interface InterfaceA {
    void methodA();
//Driver Code Ends

}

interface InterfaceB {
    void methodB();
}

// Class implements both interfaces and 
// provides implementations to the method.
class Sample implements InterfaceA, InterfaceB {
    @Override
    public void methodA() {
        System.out.println("Inside methodA");
    }

    @Override
    public void methodB() {
        System.out.println("Inside methodB");
    }
}

public class MainClass {
    public static void main(String[] args) {
        Sample s = new Sample();
        s.methodA();

//Driver Code Starts
        s.methodB();
    }
}
//Driver Code Ends

Output
Inside methodA
Inside methodB


Hierarchy Diagram

The image below demonstrates a class can extend another class and implement multiple interfaces, while an interface can extend multiple interfaces.

interface_2

Note: This hierarchy will be followed in the same way, we cannot reverse the hierarchy while inheritance in Java. This means that we cannot implement a class from the interface because Interface-to-Class inheritance is not allowed, and it goes against the fundamental principles of class-based inheritance. This will also breach the relationship known as the Is-A relationship, where a subclass is a more specialized version of its superclass.


Interface Inheritance

An Interface can extend another interface. interface_inheritance 

Inheritance is inheriting the properties of the parent class into the child class.

  • Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object.
  • The idea behind inheritance in Java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of the parent class. 
  • You can also add new methods and fields in your current class.
  • Inheritance represents the IS-A relationship which is also known as the parent-child relationship.

Example:

Dog IS_A Animal
Car IS_A Vehicle
Employee IS_A Person
Surgeon IS_A Doctor etc.

Java
// Animal is a Parent class
class Animal 
{
    public void eat()
    {
        System.out.println("Animal is eating");
    }
}

// Dog is derived from Animal class
class Dog extends Animal 
{
    public static void main(String args[])
    {
        // Creating object of Dog class
        Dog d = new Dog();

        // Dog can access eat() method
      	// of Animal class
        d.eat();
    }
}

Output
Animal is eating


Declaration of Java Inheritance

The declaration of Java inheritance is listed below:

class <Subclass-name> extends <Superclass-name> {

//methods and fields

}

Note: 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:

Java
// Parent Class
class Person {
    int id;
    String name;

    void setPerson(int id, String name) {
        this.id = id;
        this.name = name;
    }

    void displayPerson() {
        System.out.println(id);
        System.out.println(name);
    }
}

// Child Class
class Employee extends Person {
    int salary;
    String designation;

    void setEmployee(int id, String name, String designation, int salary) {
        setPerson(id, name);
        this.designation = designation;
        this.salary = salary;
    }

    void displayEmployee() {
        displayPerson();
        System.out.println(designation);
        System.out.println(salary);
    }

    public static void main(String[] args) {
        Employee e = new Employee();
        e.setEmployee(1001, "Manjeet", "AP", 20000);
        e.displayEmployee();
    }
}

Output
1001
Manjeet
AP
20000


Types of Inheritance in Java

Java supports three types of inheritance which are listed below

  • Single-level inheritance
  • Multi-level inheritance
  • hierarchical inheritance

Note: In Java programming, multiple and hybrid inheritance is supported through the interface only.

1. Single Inheritance

When a class inherits another class, it is known as a single inheritance. 

Example:

Java
// Single inheritance
// Class Child ---> Class Parent, which means
// that class Child is derived from Class Parent
class Parent {
    int value;

    void setValue(int value) {
        this.value = value;
    }
}

// Class Child have access to all public and
// protected methods and data members of Class Parent
class Child extends Parent {
    int multiplier;
    int product;

    void setMultiplier(int multiplier) {
        this.multiplier = multiplier;
    }

    void calculateProduct() {
        product = value * multiplier;
        System.out.println("Product = " + product);
    }

    public static void main(String[] args) {
        Child c = new Child();
        c.setValue(5);
        c.setMultiplier(5);
        c.calculateProduct();
    }
}

Output
Product = 25


2. Multilevel Inheritance

When there is a chain of inheritance, it is known as multilevel inheritance.

Example:

Java
// Multilevel inheritance

// Class C ---> Class B ---> Class A
// Class C is derived from Class B which
// in correspondence derived from Class A
class A {
    int valueA;

    void setValueA(int value) {
        this.valueA = value;
    }
}

// Child of Class A
class B extends A {
    int valueB;

    void setValueB(int value) {
        this.valueB = value;
    }
}

// Child of Class B but have access to
// methods of both classes, i.e., Class A and B
class C extends B {
    int product;

    void calculateProduct() {
        product = valueA * valueB;
        System.out.println("Product = " + product);
    }

    public static void main(String[] args) {
        
        // Class C accesses methods of both class A and B
        C c = new C();
        c.setValueA(5);
        c.setValueB(5);
        c.calculateProduct();
    }
}

Output
Product = 25


3. Hierarchical Inheritance

When two or more classes inherit a single class, it is known as hierarchical inheritance.

Example:

Java
// Hierarchical inheritance
// Class C ---> Class A <--- Class B
// Both Class B and C inherits Class A
class A {
    int a;
  
    void setValueA(int x) { 
      	a = x; 
        System.out.println("Setting A value to = " + x);
    }
}

// Class B derived from Class A
class B extends A 
{
    int b;
  
    void setValueB(int x) { 
      	b = x; 
      	System.out.println("Setting B value to = " + b);
    }
}

// Class C also derived from Class A
class C extends A 
{
    int c;
    void setValueC(int x) {
      	c = x;
      	System.out.println("Setting C value to = " + c);
    }
}

public class Geeks 
{
      public static void main(String[] args) 
      {
          C c = new C();
          c.setValueC(5);
          c.setValueA(50);
          
          B b = new B();
          b.setValueB(10);
          b.setValueA(15);
    }
}

Output
Setting C value to = 5
Setting A value to = 50
Setting B value to = 10
Setting A value to = 15


4. Inheritance in Interfaces

Example:

Java
// Java program to demonstrate inheritance in
// interfaces.
import java.io.*;

interface interfaceA 
{
    void geekName();
}

interface interfaceB extends interfaceA 
{
    void geekInstitute();
}

// class implements both interfaces and provides
// implementation to the method.
class sample implements interfaceB 
{
  
    @Override 
  	public void geekName() {
        System.out.println("Rohit");
    }

    @Override 
  	public void geekInstitute() {
        System.out.println("JIIT");
    }

    public static void main(String[] args)
    {
        sample s = new sample();

        // calling the method implemented
        // within the class.
        s.geekName();
        s.geekInstitute();
    }
}

Output
Rohit
JIIT


An interface can also extend multiple interfaces. 

Example:

Java
// Multiple inheritance in interfaces
import java.io.*;

interface interfaceA 
{
    void geekName();
}

interface interfaceB 
{
    void geekInstitute();
}

// always remember that interfaces always 
// extends interface but a class always 
// implements a interface
interface interfaceC extends interfaceA, interfaceB {
    void geekBranch();
}

// class implements both interfaces and provides
// implementation to the method.
class sample implements interfaceC 
{
    public void geekName() {
      	System.out.println("Rohit"); 
    }

    public void geekInstitute() {
        System.out.println("JIIT");
    }

    public void geekBranch() { 
      	System.out.println("CSE"); 
    }

    public static void main(String[] args)
    {
        sample o = new sample();

        // calling the method implemented
        // within the class.
        o.geekName();
        o.geekInstitute();
        o.geekBranch();
    }
}

Output
Rohit
JIIT
CSE

Why Multiple Inheritance is not supported through a class in Java, but it can be possible through the interface?

Multiple Inheritance is not supported by class because of ambiguity. In the case of interface, there is no ambiguity because the implementation of the method(s) is provided by the implementing class up to Java 7. From Java 8, interfaces also have implementations of methods. So if a class implements two or more interfaces having the same method signature with implementation, it is mandated to implement the method in class also.

Must Read: Java and Multiple Inheritance for more details. 


Next Article
Practice Tags :

Similar Reads