Interface in Java
An interface in Java is a blueprint that defines a contract for classes. It is used to achieve abstraction and supports multiple inheritance of type, because a class can implement multiple interfaces.
An interface is declared using the interface keyword, and a class uses the implements keyword to implement it.
Syntax
interface InterfaceName { void methodName(); } class ClassName implements InterfaceName { public void methodName() { // implementation } }
Basic Example
interface Animal { void sound(); } class Dog implements Animal { @Override public void sound() { System.out.println("Dog barks"); } } class Main { public static void main(String[] args) { Dog d = new Dog(); d.sound(); } }
Output
Dog barks
Here, Animal is an interface and Dog implements the sound() method defined by the interface.
Interface Variables
Variables declared in an interface are implicitly:
publicstaticfinal
Therefore, they act as constants.
interface Student { int AGE = 22; }
The value cannot be changed:
// Student.AGE = 25; // Error
Interface Methods
Abstract Method
Traditionally, interface methods are abstract by default:
interface Animal { void sound(); }
The implementing class must provide the method body.
Default Method
A default method can have a method body.
interface Animal { default void eat() { System.out.println("Animal is eating"); } }
Static Method
An interface can contain static methods.
interface Calculator { static int square(int n) { return n * n; } }
It can be called using the interface name:
System.out.println(Calculator.square(5));
Multiple Interfaces
A class can implement more than one interface.
interface A { void show(); } interface B { void display(); } class C implements A, B { public void show() { System.out.println("Method of A"); } public void display() { System.out.println("Method of B"); } } class Main { public static void main(String[] args) { C obj = new C(); obj.show(); obj.display(); } }
Output
Method of A Method of B
This allows Java to support multiple inheritance through interfaces.
Interface vs Abstract Class
| Interface | Abstract Class |
|---|---|
Declared using interface. | Declared using abstract class. |
Implemented using implements. | Extended using extends. |
| A class can implement multiple interfaces. | A class can extend only one class. |
| Cannot have constructors. | Can have constructors. |
Fields are implicitly public static final. | Can have instance variables. |
| Supports abstract, default, and static methods. | Can contain abstract and concrete methods. |
| Mainly used to define a contract. | Used to provide a common base class. |
Advantages of Interfaces
- Provides abstraction.
- Supports multiple inheritance of type.
- Promotes loose coupling.
- Improves code flexibility.
- Provides a common contract for different classes.
- Makes programs easier to maintain and extend.
Key Points
- An interface is declared using
interface. - A class implements an interface using
implements. - Interface methods can be abstract, default, or static.
- Interface fields are
public static final. - A class can implement multiple interfaces.
- Interfaces cannot be instantiated directly.
Conclusion
An interface defines a common contract that classes can implement. It is an important feature of Java OOP used for abstraction, flexibility, loose coupling, and multiple inheritance of type.