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:

Dog barks

 Multiple Interfaces Example

A class can implement more than one interface:


 

✔ Output:

Dog barks
Dog loves to play!

 Interface with Default Method (Java 8+)

Default methods let interfaces have method bodies.


 

✔ Output:

Car started
Beep beep!

 Static Methods in Interface


 

✔ Output:

30

 Real-Life Example

Interface works like a contract—if a company signs it, they must follow rules.


 

✔ Output:

Payment done using Credit Card
Payment done using UPI

🏁 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)

You may also like...