Java Interfaces and Multiple Inheritance – Complete Guide with Examples


Learn Java interfaces, how to implement multiple inheritance using interfaces, default and static methods, and achieve flexibility and modularity in object-oriented programming.

Interfaces and Multiple Inheritance in Java – Complete Detailed Tutorial

Interfaces in Java are abstract types that allow a class to implement multiple sets of methods, achieving multiple inheritance.

They define method signatures without implementation (except default and static methods).

1. Key Points About Interfaces

  1. Declared using the interface keyword
  2. Can have abstract methods (no body)
  3. Can have default methods with body (Java 8+)
  4. Can have static methods with body
  5. Cannot be instantiated
  6. A class can implement multiple interfaces
  7. Supports multiple inheritance, unlike classes

2. Syntax of an Interface


interface InterfaceName {
void method1(); // abstract method
void method2(); // abstract method
}
  1. Implemented using implements keyword:

class MyClass implements InterfaceName {
@Override
public void method1() {
System.out.println("Method1 implementation");
}

@Override
public void method2() {
System.out.println("Method2 implementation");
}
}

3. Example – Basic Interface Implementation


interface Vehicle {
void start();
void stop();
}

class Car implements Vehicle {
@Override
public void start() {
System.out.println("Car starts");
}

@Override
public void stop() {
System.out.println("Car stops");
}
}

public class Main {
public static void main(String[] args) {
Vehicle v = new Car();
v.start();
v.stop();
}
}

Output:


Car starts
Car stops

4. Multiple Inheritance Using Interfaces

  1. Java does not support multiple inheritance with classes to avoid ambiguity
  2. Interfaces allow multiple inheritance

Example:


interface A {
void methodA();
}

interface B {
void methodB();
}

class C implements A, B {
@Override
public void methodA() {
System.out.println("Method A implementation");
}

@Override
public void methodB() {
System.out.println("Method B implementation");
}
}

public class Main {
public static void main(String[] args) {
C obj = new C();
obj.methodA();
obj.methodB();
}
}

Output:


Method A implementation
Method B implementation

Explanation:

  1. Class C implements multiple interfaces A and B
  2. Resolves multiple inheritance issue

5. Default and Static Methods in Interfaces (Java 8+)

  1. Default methods → have body, can be overridden in implementing class
  2. Static methods → called using interface name

Example – Default & Static Methods:


interface MyInterface {
void method1(); // abstract method

default void defaultMethod() {
System.out.println("Default method in interface");
}

static void staticMethod() {
System.out.println("Static method in interface");
}
}

class MyClass implements MyInterface {
@Override
public void method1() {
System.out.println("Abstract method implemented");
}
}

public class Main {
public static void main(String[] args) {
MyClass obj = new MyClass();
obj.method1();
obj.defaultMethod(); // default method

MyInterface.staticMethod(); // static method
}
}

Output:


Abstract method implemented
Default method in interface
Static method in interface

6. Key Points of Interfaces

  1. Used for multiple inheritance
  2. Can contain abstract, default, and static methods
  3. Helps in loose coupling and modularity
  4. Promotes polymorphism (interface reference can point to multiple implementations)

7. Summary

  1. Interface → abstract type for method signatures
  2. Allows multiple inheritance, unlike classes
  3. Supports default and static methods
  4. Provides flexibility, modularity, and polymorphism in OOP