Interfaces in Java: Default Methods, Static Methods, and Multiple Inheritance


Illustration for Interfaces in Java: Default Methods, Static Methods, and Multiple Inheritance
By Last updated:

Interfaces in Java define a contract that classes must follow. They enable abstraction, loose coupling, and support multiple inheritance of type, making them one of the most powerful features in Java.


πŸ“Œ What is an Interface in Java?

  • Definition: An interface is a collection of abstract methods (and constants) that define a contract for implementing classes.
  • Why it matters: Promotes polymorphism and provides a way to achieve multiple inheritance.
  • When to use: When you want different classes to share behavior without enforcing a strict class hierarchy.

[Related: link-to-other-article]


πŸ”Ή Syntax of an Interface

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

Implementing an Interface:

class Car implements Vehicle {
    public void start() {
        System.out.println("Car starting");
    }
    public void stop() {
        System.out.println("Car stopping");
    }
}

πŸ”Ή Default Methods in Interfaces

  • Introduced in Java 8.
  • Allow interfaces to have methods with a body, providing backward compatibility.

πŸ’» Example:

interface Vehicle {
    void start();
    default void honk() {
        System.out.println("Default honk sound");
    }
}

πŸ”Ή Static Methods in Interfaces

  • Also added in Java 8.
  • Can be called without creating an instance or implementing class.

πŸ’» Example:

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

public class Main {
    public static void main(String[] args) {
        Utils.displayInfo();
    }
}

πŸ”Ή Multiple Inheritance with Interfaces

Java classes can implement multiple interfaces, enabling multiple inheritance of type.

interface A { void methodA(); }
interface B { void methodB(); }

class MyClass implements A, B {
    public void methodA() { System.out.println("A"); }
    public void methodB() { System.out.println("B"); }
}

πŸ”Ή Real-World Analogy

Think of an interface as a job description. Multiple candidates (classes) can apply (implement) it, but each may have their own way of fulfilling the job.


πŸ“Š Interface vs Abstract Class

Feature Interface Abstract Class
Methods Abstract, default, static Abstract + concrete
Variables Public static final Instance + static
Multiple Inheritance Supported Not supported
Access Modifiers Methods are public by default Can be any

🚫 Common Mistakes and Anti-Patterns

  • ❌ Forgetting to declare methods as public when implementing (they are implicitly public).
  • ❌ Overusing interfaces for single-class implementations.
  • ❌ Mixing unrelated behaviors in one interface (violates Interface Segregation Principle).

πŸ“ˆ Performance and Memory Implications

  • Interfaces add minimal overhead; dynamic method dispatch is used at runtime.
  • Static and default methods are optimized by the JVM.

πŸ”§ Best Practices

  • Use interfaces to define contracts, not state.
  • Keep interfaces focused and cohesive.
  • Use default methods for backward compatibility, not to add heavy logic.

πŸ“š Interview Questions

  1. Q: Can interfaces have constructors?
    A: No, they cannot have constructors.

  2. Q: What’s the difference between default and static methods in interfaces?
    A: Default methods are inherited by implementing classes; static methods belong to the interface only.

  3. Q: Can an interface extend another interface?
    A: Yes, interfaces can extend multiple other interfaces.


πŸ“Œ Java Version Relevance

Java Version Change
Java 1.0 Interfaces introduced
Java 8 Added default and static methods
Java 9 Introduced private methods in interfaces

βœ… Conclusion & Key Takeaways

  • Interfaces define contracts and enable multiple inheritance in Java.
  • Default and static methods improve flexibility and backward compatibility.
  • Keep interfaces simple and cohesive for better design.

❓ FAQ

Q: Can interfaces have fields?
A: Yes, but they are implicitly public static final.

Q: Can we achieve multiple inheritance using interfaces?
A: Yes, a class can implement multiple interfaces.

Q: Are interface methods abstract by default?
A: Yes, unless declared as default or static.

πŸ“– Part of a Series

This tutorial is part of our Core Java . Explore the full guide for related topics, explanations, and best practices.

β†’ View all tutorials in this series