Abstraction
Interfaces
Deconstruct interface contracts, default implicit modifiers, multiple inheritance, and API isolation.
Interview: Evaluates implicit variable modifiers (public static final), multiple interface inheritance capabilities, and class implementing contracts.
An Interface in Java is a reference type that specifies a contract of capabilities. It defines behavior specifications that implementing classes must fulfill, enabling multiple inheritance and complete separation of API from implementation.
Implicit Modifiers
All interface fields are implicitly public static final. All abstract methods are implicitly public abstract.
Multiple Inheritance
While a class can extend only one superclass, it can implement multiple interfaces, allowing objects to adopt multiple capability types.
Interface Chaining
An interface can extend multiple sibling interfaces (e.g. interface A extends B, C), consolidating behavior specifications.
Implicit Variable and Method Modifiers
The Java compiler automatically applies specific modifiers to interface elements, which are often omitted for brevity:
- Interface Fields: Automatically compile to
public static final. They are global constants and must be initialized immediately at declaration. Interfaces cannot hold instance variables. - Abstract Methods: Automatically compile to
public abstract. Implementing classes must override them withpublicvisibility, as narrowing the modifier yields compile errors.
Multiple Interface Inheritance
Interfaces allow Java to achieve multiple inheritance of behavior. A class declaration can state:
class SmartPhone implements Phone, Camera, GPS { ... }
Because interfaces contain no instance state (no fields that store values on individual objects), implementing multiple interfaces avoids the diamond state conflicts associated with multiple class inheritance.
Common Pitfalls
- Omitting public in implementer: Declaring the overridden method in the class without the
publicmodifier (e.g. package-private), causing a compile error due to access narrowing. - Interface bloating: Adding new abstract methods to an active production interface, which immediately breaks all classes implementing that interface.
Best Practices
- Interface Segregation Principle: Keep interfaces small and cohesive (e.g., separating
SerializablefromReadable). - Avoid Field Constants in Interfaces: Avoid using interfaces as constant registries (the Constant Interface anti-pattern). Use helper classes or enums for constants instead.
Interview-Relevant Information
Q1: What are the default modifiers of fields and methods in a Java interface?
Answer: Interface fields are implicitly public static final. Methods (without body) are implicitly public abstract. Even if you write void doWork();, the compiler compiles it as public abstract void doWork();.
Q2: Can an interface extend multiple interfaces?
Answer: Yes. Unlike classes which support only single inheritance, an interface can extend multiple other interfaces using the syntax: interface ChildInterface extends ParentA, ParentB { ... }.
Quick Checklist
Can you state the implicit modifiers of interface variables, write a class implementing multiple interfaces, and extend multiple interfaces simultaneously? If yes, you understand interfaces.
Use Cases
Establishing cross-cutting capability contracts across unrelated class domains (e.g. Runnable, Comparable).
Creating architectural layer isolation (e.g. Service layer implementing ServiceInterface).
Common Mistakes
Declaring interface fields without initialization values, resulting in compilation failures.
Narrowing the access modifier to default or protected when implementing interface methods in concrete classes.