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
publicwhen 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
-
Q: Can interfaces have constructors?
A: No, they cannot have constructors. -
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. -
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.