Interfaces and Inheritance in Java

Last Updated : 10 Oct, 2025

inheritance and interface enable code reusability, polymorphism and abstraction. Though they may seem similar, they serve different purposes in Java.

  • Inheritance is a mechanism by which a class (called subclass) can inherit data and methods from another class (called superclass).
  • An interface defines a contract (a set of methods/signatures) without (necessarily) giving full implementation.

Inheritance in Java

It promotes code reusability and forms a hierarchical relationship between classes. We also achieve run time polymorphism through inheritance and overriding.

Types of Inheritance

  1. Single Inheritance: One class inherits from another.
  2. Multilevel Inheritance: A class inherits from a subclass, forming a chain.
  3. Hierarchical Inheritance: Multiple classes inherit from the same parent class.

Note: Java does not support Multiple Inheritance with classes to avoid ambiguity.

Java
class Parent{
    
    void display(){
        
        System.out.println("This is the parent class");
    }
}

class Child extends Parent{
    
    void show(){
        
        System.out.println("This is the child class");
    }
}

public class TestInheritance{
    
    public static void main(String[] args){
        
        Child c = new Child();
        c.display(); 
        c.show();
    }
}

Output
This is the parent class
This is the child class

Explanation: The Child class inherits the display() method from Parent and can also have its own methods.

Interfaces in Java

Interface encourages abstraction (implementation not provided) and loose coupling, and allows multiple different types to be used interchangeably via the interface. Through interfaces, we can get polymorphism: as we can call interface methods on an object without knowing its concrete class, as long as it implements the interface.

Java
interface Drawable{
    
    // abstract method
    void draw(); 
}

interface Colorable{
    
    void setColor(String color);
}

class Circle implements Drawable, Colorable{
    
    private String color;

    @Override
    public void draw(){
        
        System.out.println("Drawing a circle");
    }

    @Override
    public void setColor(String color){
        
        this.color = color;
        System.out.println("Circle color set to: " + color);
    }
}

public class TestInterface{
    
    public static void main(String[] args){
        
        Circle c = new Circle();
        c.draw();
        c.setColor("Red");
    }
}

Output
Drawing a circle
Circle color set to: Red

Explanation: The Circle class implements both Drawable and Colorable interfaces and provides concrete implementations for their methods. This allows multiple inheritance of type, which is not possible with classes.

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

Interface Inheritance

An interface can extend another interface, and a class implementing it must provide implementations for all abstract methods.interface_inheritance

Java
interface InterfaceA{
    
    void geekName();
}

interface InterfaceB extends InterfaceA{
    
    void geekInstitute();
}

class Sample implements InterfaceB{
    
    public void geekName(){
        
        System.out.println("Rohit");
    }

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

    public static void main(String[] args){
        
        Sample s = new Sample();
        s.geekName();
        s.geekInstitute();
    }
}

Output
Rohit
JIIT

Multiple Inheritance with Interfaces

Interfaces can extend multiple interfaces, unlike classes.

Java
interface InterfaceA{
    
    void geekName();
}

interface InterfaceB{
    
    void geekInstitute();
}

interface InterfaceC extends InterfaceA, InterfaceB{
    
    void geekBranch();
}

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();
        o.geekName();
        o.geekInstitute();
        o.geekBranch();
    }
}

Output
Rohit
JIIT
CSE

Difference table for Inheritance vs Interface

FeatureInheritance (Class)Interface
PurposeReuse code from a parent classDefine a contract or behavior a class must follow
Relationship TypeIS-ACAN-DO
Multiple InheritanceNot allowed with classesAllowed (a class can implement multiple interfaces)
MethodsCan have concrete and abstract methodsMethods are abstract by default (can have default/static from Java 8)
Access ModifiersMethods can have any access modifierMethods are public by default
Use CaseWhen classes share common structure or behaviorWhen unrelated classes need to follow a common behavior or contract
Code ReusabilityProvides implementation reuseProvides behavioral contract, but not implementation


Comment