Java Interface
Java Interface
An interface in Java is a blueprint of a class. It defines a set of abstract methods (behaviors) that any implementing class must follow.
Interfaces help achieve abstraction and multiple inheritance in Java.
Key Points About Interfaces
| Feature | Description |
|---|---|
| Contains abstract methods | Yes (before Java 8: all methods were abstract) |
| Supports multiple inheritance | ✔ Yes |
| Objects allowed? | ❌ Cannot create objects |
| Variables | Always public static final (constant) |
| Methods | Can be abstract, default, static, and private (after Java 8) |
🧩 Basic Interface Example
✔ Output:
Multiple Interfaces Example
A class can implement more than one interface:
✔ Output:
Interface with Default Method (Java 8+)
Default methods let interfaces have method bodies.
✔ Output:
Static Methods in Interface
✔ Output:
Real-Life Example
Interface works like a contract—if a company signs it, they must follow rules.
✔ Output:
🏁 When to Use Interface?
Use interface when:
✔ Multiple classes should follow the same rules
✔ You need multiple inheritance
✔ You want total abstraction
🎉 Summary
| Feature | Yes / No |
|---|---|
| Supports multiple inheritance | ✔ |
| Object creation allowed | ❌ |
| Contains only abstract methods (before Java 8) | ✔ |
| Can contain default and static methods (after Java 8) | ✔ |
