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
- Single Inheritance: One class inherits from another.
- Multilevel Inheritance: A class inherits from a subclass, forming a chain.
- Hierarchical Inheritance: Multiple classes inherit from the same parent class.
Note: Java does not support Multiple Inheritance with classes to avoid ambiguity.
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.
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 Inheritance
An interface can extend another interface, and a class implementing it must provide implementations for all abstract methods.
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.
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
| Feature | Inheritance (Class) | Interface |
|---|---|---|
| Purpose | Reuse code from a parent class | Define a contract or behavior a class must follow |
| Relationship Type | IS-A | CAN-DO |
| Multiple Inheritance | Not allowed with classes | Allowed (a class can implement multiple interfaces) |
| Methods | Can have concrete and abstract methods | Methods are abstract by default (can have default/static from Java 8) |
| Access Modifiers | Methods can have any access modifier | Methods are public by default |
| Use Case | When classes share common structure or behavior | When unrelated classes need to follow a common behavior or contract |
| Code Reusability | Provides implementation reuse | Provides behavioral contract, but not implementation |