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
- Declared using the
interfacekeyword - Can have abstract methods (no body)
- Can have default methods with body (Java 8+)
- Can have static methods with body
- Cannot be instantiated
- A class can implement multiple interfaces
- Supports multiple inheritance, unlike classes
2. Syntax of an Interface
interface InterfaceName {
void method1(); // abstract method
void method2(); // abstract method
}
- Implemented using
implementskeyword:
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
- Java does not support multiple inheritance with classes to avoid ambiguity
- 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:
- Class
Cimplements multiple interfacesAandB - Resolves multiple inheritance issue
5. Default and Static Methods in Interfaces (Java 8+)
- Default methods → have body, can be overridden in implementing class
- 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
- Used for multiple inheritance
- Can contain abstract, default, and static methods
- Helps in loose coupling and modularity
- Promotes polymorphism (interface reference can point to multiple implementations)
7. Summary
- Interface → abstract type for method signatures
- Allows multiple inheritance, unlike classes
- Supports default and static methods
- Provides flexibility, modularity, and polymorphism in OOP