Interfaces
An interface in Java defines a contract that a class must follow. It specifies what a class can do, not how it does it. Interfaces are fundamental to abstraction, loose coupling, multiple inheritance, and API design. This is a very high-frequency interview topic, especially when compared with abstract classes.
What Is an Interface?
- A blueprint that contains method declarations (contracts)
- Implemented by classes using the implements keyword
- Supports multiple inheritance
- Promotes loose coupling
interface Payment {
void pay();
}
Why Interfaces Are Needed
- Achieve full abstraction
- Support multiple inheritance
- Enable plug-and-play design
- Improve testability and flexibility
- Foundation of frameworks (Spring, JDBC, Collections)
Basic Syntax
interface InterfaceName {
returnType methodName();
}
class ClassName implements InterfaceName {
public returnType methodName() {
// implementation
}
}
Simple Interface Example
interface Animal {
void sound();
}
class Dog implements Animal {
@Override
public void sound() {
System.out.println("Dog barks");
}
}
Animal a = new Dog();
a.sound();
✔ Runtime polymorphism
✔ Loose coupling
Key Rules of Interfaces (Very Important)
- Methods Are public and abstract by Default
interface Test {
void show(); // public abstract implicitly
}
- Variables Are public static final by Default
interface Config {
int MAX = 100; // constant
}
- ✔ Must be initialized
- ✔ Cannot be changed
- Interface Cannot Be Instantiated
// Test t = new Test(); // ❌
- A Class Must Implement All Interface Methods
class A implements Test {
public void show() { }
}
✔ Otherwise, class must be declared abstract
Multiple Inheritance Using Interfaces (Key Advantage)
interface A {
void show();
}
interface B {
void display();
}
class C implements A, B {
public void show() { }
public void display() { }
}
✔ Solves the diamond problem safely
✔ One of Java’s strongest design features
Interface Reference and Polymorphism
Payment p = new CreditCardPayment();
p.pay();
- Reference type → Interface
- Object type → Implementation class
- Method call resolved at runtime
Interface Inheritance (extends)
An interface can extend another interface.
interface A {
void show();
}
interface B extends A {
void display();
}
✔ Supports multiple inheritance between interfaces
Java 8+ Interface Enhancements (Interview Favorite)
- Default Methods
Allow method implementation inside interface.
interface Vehicle {
default void start() {
System.out.println("Vehicle starts");
}
}
✔ Prevents breaking existing implementations
- Static Methods
interface Utility {
static void help() {
System.out.println("Helping");
}
}
✔ Called using interface name only
- Functional Interfaces (Preview)
@FunctionalInterface
interface Calculator {
int add(int a, int b);
}
✔ Used with Lambda expressions
Interface vs Abstract Class (Interview Favorite)
| Feature | Interface | Abstract Class |
|---|---|---|
| Abstraction | Full | Partial |
| Methods | Abstract + default | Abstract + concrete |
| Variables | Constants only | Instance variables |
| Constructors | ❌ No | ✅ Yes |
| Multiple inheritance | ✅ Yes | ❌ No |
| Use case | Contract / capability | Base class |
When to Use Interfaces
- When multiple inheritance is required
- When defining a contract
- When building APIs/frameworks
- When behavior varies across implementations
- When loose coupling is needed
When NOT to Use Interfaces
- When shared state is required
- When common implementation is large
- When constructor logic is needed
Common Beginner Mistakes
- Forgetting methods are public
- Trying to create objects of interface
- Not implementing all methods
- Confusing interface with abstract class
- Overusing default methods
Interview-Ready Answers
Short Answer
An interface defines a contract that a class must implement.
Detailed Answer
In Java, an interface provides full abstraction by declaring methods without implementation. Classes implement interfaces using the implements keyword. Interfaces support multiple inheritance, polymorphism, and are widely used for designing flexible and loosely coupled systems.
Top 20 Interface Examples (With Output)
1. Basic Interface Implementation
interface Animal {
void sound();
}
class Dog implements Animal {
public void sound() {
System.out.println("Dog barks");
}
public static void main(String[] args) {
Animal a = new Dog();
a.sound();
}
}
Output
Dog barks
2. Interface Reference → Runtime Polymorphism
interface Shape {
void draw();
}
class Circle implements Shape {
public void draw() {
System.out.println("Circle");
}
}
class Test {
public static void main(String[] args) {
Shape s = new Circle();
s.draw();
}
}
Output
Circle
3. Multiple Inheritance Using Interfaces
interface A {
void a();
}
interface B {
void b();
}
class C implements A, B {
public void a() { System.out.println("A"); }
public void b() { System.out.println("B"); }
public static void main(String[] args) {
C c = new C();
c.a();
c.b();
}
}
Output
A
B
4. Interface with Default Method (Java 8+)
interface Printer {
default void print() {
System.out.println("Default print");
}
}
class LaserPrinter implements Printer {
public static void main(String[] args) {
new LaserPrinter().print();
}
}
Output
Default print
5. Overriding Default Method
interface Printer {
default void print() {
System.out.println("Default");
}
}
class InkPrinter implements Printer {
public void print() {
System.out.println("Ink Printer");
}
public static void main(String[] args) {
new InkPrinter().print();
}
}
Output
Ink Printer
6. Interface with Static Method
interface Utils {
static void help() {
System.out.println("Helping");
}
}
class Test {
public static void main(String[] args) {
Utils.help();
}
}
Output
Helping
7. Static Method Not Inherited
interface A {
static void show() {
System.out.println("A");
}
}
class B implements A {
public static void main(String[] args) {
// show(); // ❌ Compile-time error
A.show();
}
}
Explanation
Interface static methods must be called using interface name.
8. Interface Variables Are public static final by Default
interface Config {
int TIMEOUT = 30;
}
class Test {
public static void main(String[] args) {
System.out.println(Config.TIMEOUT);
}
}
Output
30
9. Cannot Modify Interface Variable
interface Config {
int TIMEOUT = 30;
}
class Test {
public static void main(String[] args) {
// Config.TIMEOUT = 40; // ❌ Compile-time error
}
}
Explanation
Interface fields are constants.
10. Interface with Multiple Implementations
interface Payment {
void pay();
}
class Card implements Payment {
public void pay() { System.out.println("Card Payment"); }
}
class UPI implements Payment {
public void pay() { System.out.println("UPI Payment"); }
}
class Test {
public static void main(String[] args) {
Payment p1 = new Card();
Payment p2 = new UPI();
p1.pay();
p2.pay();
}
}
Output
Card Payment
UPI Payment
11. Interface Used as Method Parameter
interface Logger {
void log(String msg);
}
class FileLogger implements Logger {
public void log(String msg) {
System.out.println("File: " + msg);
}
}
class Test {
static void process(Logger l) {
l.log("Started");
}
public static void main(String[] args) {
process(new FileLogger());
}
}
Output
File: Started
12. Interface Used as Return Type
interface Browser {
void open();
}
class Chrome implements Browser {
public void open() {
System.out.println("Chrome opened");
}
}
class Factory {
static Browser getBrowser() {
return new Chrome();
}
public static void main(String[] args) {
Browser b = getBrowser();
b.open();
}
}
Output
Chrome opened
13. Interface Extending Another Interface
interface A {
void a();
}
interface B extends A {
void b();
}
class C implements B {
public void a() { System.out.println("A"); }
public void b() { System.out.println("B"); }
public static void main(String[] args) {
C c = new C();
c.a();
c.b();
}
}
Output
A
B
14. Class Implementing Multiple Interfaces with Same Method
interface A {
void show();
}
interface B {
void show();
}
class C implements A, B {
public void show() {
System.out.println("Single implementation");
}
public static void main(String[] args) {
new C().show();
}
}
Output
Single implementation
15. Diamond Problem with Default Methods
interface A {
default void show() {
System.out.println("A");
}
}
interface B {
default void show() {
System.out.println("B");
}
}
class C implements A, B {
public void show() {
A.super.show();
}
public static void main(String[] args) {
new C().show();
}
}
Output
A
16. Interface Cannot Have Constructors
interface A {
// A() {} // ❌ Compile-time error
}
Explanation
Interfaces cannot be instantiated.
17. Functional Interface (Single Abstract Method)
@FunctionalInterface
interface Calc {
int add(int a, int b);
}
class Test {
public static void main(String[] args) {
Calc c = (x, y) -> x + y;
System.out.println(c.add(2, 3));
}
}
Output
5
18. Interface vs Abstract Class (State Difference)
interface I {
// int x; // ❌ must be initialized
}
abstract class A {
int x;
}
Explanation
Interface → constants only
Abstract class → instance variables allowed
19. Real-World Example (WebDriver-Style Interface)
interface Driver {
void start();
}
class ChromeDriver implements Driver {
public void start() {
System.out.println("Chrome Driver started");
}
public static void main(String[] args) {
Driver d = new ChromeDriver();
d.start();
}
}
Output
Chrome Driver started
20. Interview Summary – Interfaces
interface Service {
void execute();
}
class Impl implements Service {
public void execute() {
System.out.println("Executed");
}
public static void main(String[] args) {
Service s = new Impl();
s.execute();
}
}
Key Points
- Achieves full abstraction
- Supports multiple inheritance
- Enables loose coupling & polymorphism
Output
Executed
Key Takeaway
Interfaces define “what to do,” not “how to do it.” They are the backbone of scalable, extensible, and testable Java applications.