Interface in Java - Contracts, Capabilities and Multiple Inheritance
⏱️ 24 min read • Beginner Level • Lesson 58
Lesson 58 of 124 of Java Tutorial
You have completed this lesson
Completed on .
You can revise this lesson or continue to the next topic.
An interface in Java defines a contract or capability that implementing classes agree to provide.
Interfaces help separate what a component can do from how it does it. They support abstraction, loose coupling, runtime polymorphism, multiple inheritance of type, focused APIs, and interchangeable implementations.
What Is an Interface in Java?
An interface is a reference type that describes operations, constants, and supported method implementations. A class uses the implements keyword to accept an interface contract.
Simple Meaning: An interface says what behavior must be available, while implementing classes decide how that behavior works.
Interfaces are useful for:
Defining contracts shared by different classes.
Programming to capabilities instead of concrete implementations.
Supporting runtime polymorphism.
Allowing one class to implement multiple interfaces.
Making dependencies easier to replace and test.
Real-World Analogy
A wall socket defines a standard connection. Different devices can use that connection while their internal electronics remain different. Similarly, an interface defines a standard contract that different classes can implement in their own way.
Socket Standard
Represents the interface contract.
Different Devices
Represent different implementing classes.
Compatible Use
Client code uses the common standard.
Java Interface Syntax
Syntax.java
interface InterfaceName {
void requiredMethod();
}
class ClassName implements InterfaceName {
@Override
public void requiredMethod() {
// implementation
}
}
Visibility rule: An ordinary interface method is public. Its implementation in a class must therefore be declared public; weaker access causes a compilation error.
notifyUser() depends on the interface instead of a specific service.
A new implementation can be added without rewriting the client method.
Members Allowed in an Interface
Member
Allowed?
Notes
Abstract method
Yes
Ordinary declarations are implicitly public and abstract.
Default method
Yes
Has a body and is inherited by implementing classes.
Static method
Yes
Called using the interface name.
Private method
Yes
Used internally by interface methods in modern Java.
Fields
Yes
They are implicitly public, static, and final.
Constructor
No
Interfaces do not create object state.
Instance field
No
Declared fields are constants, not per-object state.
Interface Fields
Every field declared in an interface is implicitly public static final and must be initialized.
Configuration.java
interface Configuration {
int MAX_RETRIES = 3;
String APP_NAME = "ProwessApps";
}
class ConfigDemo {
public static void main(String[] args) {
System.out.println(Configuration.APP_NAME);
System.out.println(Configuration.MAX_RETRIES);
}
}
Output:
ProwessApps
3
Explanation
MAX_RETRIES and APP_NAME are implicitly public static final.
They are accessed directly using the interface name (Configuration.APP_NAME).
Their values cannot be changed because they are constants.
Abstract Interface Methods
A method declared without a body in an interface is implicitly public abstract. The implementing class must provide a public implementation unless it is abstract or inherits a valid implementation.
Writing public abstract explicitly is allowed, but usually unnecessary because Java supplies those modifiers automatically.
Default Methods
A default method has a body and is inherited by implementing classes. Default methods help interfaces evolve by adding behavior without immediately breaking every existing implementation.
printHeader() is a default method with a full implementation.
ConsolePrinter inherits printHeader() without needing to implement it.
This feature allows adding new methods to an interface without breaking existing code.
Note: Default methods were introduced in Java 8 to allow interfaces to evolve without breaking backward compatibility.
Static Methods in Interfaces
A static interface method belongs to the interface and is called with the interface name. It is not inherited as an instance method by implementing classes.
Validator.java
interface Validator {
static boolean isPositive(int number) {
return number > 0;
}
}
class ValidatorDemo {
public static void main(String[] args) {
System.out.println(Validator.isPositive(10));
}
}
Output:
true
Explanation
isPositive() is a static method belonging strictly to the Validator interface.
It is called using Validator.isPositive(10) instead of creating an object.
Static interface methods are not inherited by implementing classes.
Note: Static methods in interfaces were introduced in Java 8, making it easier to provide utility methods related to the interface.
Private Methods in Interfaces
Modern Java interfaces can use private instance and private static methods to reuse internal logic between default and static methods. Implementing classes cannot call or override these private helpers.
Note: Private methods in interfaces were introduced in Java 9 to help share code between default and static methods.
Implementing Multiple Interfaces
A Java class can implement multiple interfaces. This allows one class to support several independent capabilities without extending multiple classes.
SmartphoneExample.java
interface Camera {
void takePhoto();
}
interface MusicPlayer {
void playMusic();
}
class Smartphone implements Camera, MusicPlayer {
@Override
public void takePhoto() {
System.out.println("Photo captured");
}
@Override
public void playMusic() {
System.out.println("Music playing");
}
public static void main(String[] args) {
Smartphone phone = new Smartphone();
phone.takePhoto();
phone.playMusic();
}
}
Output:
Photo captured
Music playing
Explanation
Smartphone implements both Camera and MusicPlayer.
It provides concrete implementations for the abstract methods from both interfaces.
This is how Java achieves Multiple Inheritance of Type.
One Interface Extending Another
An interface uses extends to inherit from one or more interfaces. A class implementing the child interface must satisfy the complete inherited contract.
MultiFunctionDevice extends two interfaces: Printable and Scannable.
A class implementing MultiFunctionDevice must provide implementations for print(), scan(), and copy().
Interfaces can extend multiple interfaces using the extends keyword.
Resolving Default-Method Conflicts
If two implemented interfaces provide default methods with the same signature, the implementing class must override the method and resolve the conflict explicitly.
ConflictExample.java
interface A {
default void show() {
System.out.println("A");
}
}
interface B {
default void show() {
System.out.println("B");
}
}
class ConflictExample implements A, B {
@Override
public void show() {
A.super.show();
B.super.show();
System.out.println("Resolved");
}
public static void main(String[] args) {
new ConflictExample().show();
}
}
Output:
A
B
Resolved
Explanation
Interfaces A and B both provide a default show() method.
ConflictExample overrides show() to resolve the compilation error caused by this ambiguity.
It uses A.super.show() and B.super.show() to explicitly call the default implementations.
Functional Interfaces
A functional interface has exactly one abstract method. It may also contain default, static, and private methods. Functional interfaces are targets for lambda expressions and method references.
CalculatorExample.java
@FunctionalInterface
interface Calculator {
int calculate(int first, int second);
}
class CalculatorExample {
public static void main(String[] args) {
Calculator add = (a, b) -> a + b;
System.out.println(add.calculate(10, 20));
}
}
Output:
30
Explanation
Calculator is a functional interface because it has exactly one abstract method (calculate).
The @FunctionalInterface annotation explicitly marks it and tells the compiler to enforce this rule.
Because it's a functional interface, it can be implemented concisely using a Lambda expression: (a, b) -> a + b.
Note: Functional interfaces and Lambda expressions were introduced in Java 8.
Interface References and Polymorphism
An interface reference can point to any object whose class implements that interface. The actual object's overridden method executes at runtime.
The reference exposes members available through the interface type.
The concrete object supplies the runtime implementation.
Implementations can be replaced without changing contract-based client code.
Interface vs Abstract Class
Feature
Interface
Abstract Class
Purpose
Defines a capability or contract.
Defines a shared base for related classes.
Instance state
No normal instance fields.
Supports instance fields.
Constructors
Not allowed.
Allowed.
Methods
Abstract, default, static, and private methods.
Abstract and concrete methods with normal valid access levels.
Multiple use
A class may implement multiple interfaces.
A class can extend only one class.
Best fit
Potentially unrelated classes sharing behavior.
Closely related classes sharing state or implementation.
Important Interface Rules
An interface cannot be instantiated directly.
A concrete implementing class must implement required abstract methods.
Implementations of ordinary interface methods must be public.
Interface fields are implicitly public, static, and final.
Interfaces do not have constructors or normal instance fields.
A class may implement multiple interfaces.
An interface may extend multiple interfaces.
Default methods may be overridden.
Static interface methods are called through the interface name.
Private interface methods are internal helpers and cannot be overridden.
Interface Best Practices
Keep each interface focused on one coherent capability.
Prefer behavior-oriented names such as Comparable, Runnable, or Exportable.
Depend on interfaces when multiple implementations or test replacements are useful.
Use default methods carefully; avoid turning an interface into a large implementation container.
Avoid constant-only interfaces; use a final utility/configuration class or enum when more appropriate.
Do not add methods casually to public interfaces because implementers may be affected.
Tricky Cases
1. Can an interface have a constructor?
No. Interfaces do not represent constructible object state.
2. Can an interface have private methods?
Yes, in modern Java. They are internal helpers for default, private, or static interface methods as permitted.
3. Are interface methods always abstract?
No. Interfaces can also contain default, static, and private methods with bodies.
4. Can a class implement two interfaces with the same abstract method?
Yes. One compatible public implementation can satisfy both contracts.
5. What if two interfaces provide the same default method?
The implementing class must override the method to resolve the conflict.
6. Can an interface extend a class?
No. An interface can extend one or more interfaces, not a class.
7. Can an abstract class implement an interface partially?
Yes. A concrete subclass must complete any remaining abstract methods.
Common Mistakes
Trying to instantiate an interface.
Using extends instead of implements when a class adopts an interface.
Giving an implemented interface method weaker than public access.
Trying to change an interface constant.
Calling an interface static method through an implementing object.
Ignoring conflicts between inherited default methods.
Creating large interfaces with unrelated responsibilities.
Assuming interfaces can never contain implemented methods.
Summary:
An interface defines a contract or capability.
A class adopts an interface with implements.
Interface fields are public static final constants.
Interfaces may contain abstract, default, static, and private methods.
A class can implement multiple interfaces.
An interface can extend multiple interfaces.
Interface references support runtime polymorphism.
Functional interfaces work with lambda expressions.
Use focused interfaces to achieve loose coupling and extensibility.
Interview Questions ⭐
An interface is a reference type that defines a contract or capability that implementing classes agree to provide.
The interface keyword is used to declare an interface.
A class uses the implements keyword to adopt an interface contract.
No. An interface cannot be instantiated directly, but an interface reference can refer to an object of an implementing class.
Yes. A Java class can implement multiple interfaces.
Yes. An interface can extend one or more interfaces.
No. An interface can extend interfaces, not classes.
Fields declared in an interface are implicitly public, static, and final.
Yes. Interface fields are constants and must have an initializer.
An interface method declared without a body and without another permitted modifier is implicitly public and abstract.
The interface method is public, so its implementation cannot reduce visibility.
Yes. A default method has a body and is inherited by implementing classes unless it is overridden.
Default methods allow interfaces to add behavior while reducing disruption to existing implementing classes.
Yes. An implementing class can override an inherited default method.
Yes. Static interface methods belong to the interface and are called using the interface name.
No. They are called through the interface name rather than through an implementing object.
Yes. Modern Java interfaces can contain private instance and private static helper methods.
No. Private interface methods are internal helpers and are not accessible to implementing classes.
No. Interfaces do not create object instances or maintain normal per-object state, so they do not have constructors.
No. Fields declared in an interface are public static final constants, not instance fields.
The compiler reports an error unless the class is declared abstract or valid implementations are already inherited.
Yes. An abstract class may leave some interface methods for a concrete subclass to implement.
Yes. If the method signatures are compatible, one public implementation can satisfy both contracts.
The implementing class must override the method to resolve the conflict.
Inside the implementing class, it can use InterfaceName.super.methodName() when Java's interface-super rules permit it.
A functional interface has exactly one abstract method and can be used as the target type of a lambda expression or method reference.
Yes. It may contain any number of default, static, and private methods while retaining exactly one abstract method.
No. The annotation is optional, but it lets the compiler verify that the interface satisfies the functional-interface rules.
An interface reference is a variable whose type is an interface and which can refer to an object of any compatible implementing class.
Client code can use an interface reference while the actual implementing object's overridden method executes at runtime.
An interface primarily defines a capability and supports multiple implementation. An abstract class can provide constructors, normal instance state, protected helpers, and substantial shared implementation for related subclasses.
Prefer an interface when potentially unrelated classes need to share a capability, multiple contracts are useful, or client code should depend on replaceable implementations.
No. An interface is designed to be implemented or extended, while final would prevent extension.
No. A top-level interface can be public or package-private. A nested interface may use access modifiers permitted by its declaration context.
A member interface is implicitly static, so it does not require an instance of the enclosing class.
Yes. An interface can declare a public static main method with a body, and it can be launched by the Java runtime.
A marker interface declares no methods and marks implementing classes as having a particular characteristic, although annotations are often used for similar metadata today.
A focused interface is easier to understand, implement, test, and evolve, and it avoids forcing classes to depend on unrelated operations.
Usually no. Constant-only interfaces expose constants as an inherited API; a final utility class, configuration type, or enum is often clearer.
Depending on an interface reduces coupling and makes implementations easier to replace, extend, mock, and test.
Q2. Which statement best distinguishes an interface from an abstract class?
Q3. Can an interface be instantiated directly?
Q4. Which interface method has a body and is inherited by implementing classes?
Q5. What is the output of the lambda example?
@FunctionalInterface
interface Calculator {
int calculate(int a, int b);
}
class Test {
public static void main(String[] args) {
Calculator add = (a, b) -> a + b;
System.out.println(add.calculate(10, 20));
}
}
Q6. Can a modern Java interface contain private helper methods?
Q7. How is a static interface method normally called?
Q8. Which keyword does a class use to adopt an interface?
Q9. Which keyword declares an interface in Java?
Q10. Can an interface have a constructor?
Q11. Can a Java class implement multiple interfaces?
Q12. Which design is the best use of an interface?
Q13. What are fields declared in an interface implicitly?
Q14. Which statement about static interface methods is correct?
Q15. What is a functional interface?
Q16. What happens when this implementation is compiled?
Discussion
Ask questions, share suggestions, or discuss this lesson.