What is an Interface in Java?
An Interface in Java is a blueprint of a class that contains abstract methods, default methods, static methods, and constants. It is mainly used to achieve:
- Abstraction
- Multiple Inheritance
- Loose Coupling
- Standardization
An interface defines what a class should do, not how it should do it.
Why Do We Use Interfaces?
Interfaces are used when:
- Multiple classes need the same behavior
- You want to achieve abstraction
- You want to support multiple inheritance
- You want loose coupling in applications
- You are designing APIs or frameworks
Syntax of Interface
interface InterfaceName
{
void method1();
void method2();
}
Implementing an Interface
A class uses the implements keyword to implement an interface.
interface Animal
{
void sound();
}
class Dog implements Animal
{
public void sound()
{
System.out.println("Dog barks");
}
}
public class Main
{
public static void main(String[] args)
{
Dog d = new Dog();
d.sound();
}
}
Output
Dog barks
Important Rules of Interface
1. Interface Methods are Public and Abstract by Default
interface Test
{
void show();
}
Internally:
public abstract void show();
2. Variables are Public Static Final by Default
interface Demo
{
int x = 10;
}
Internally:
public static final int x = 10;
You cannot change the value.
Example of Interface Variables
interface Demo
{
int x = 100;
}
class Test implements Demo
{
public static void main(String[] args)
{
System.out.println(x);
}
}
Output
100
Multiple Interfaces in Java
Java does not support multiple inheritance with classes, but supports it using interfaces.
interface A
{
void show();
}
interface B
{
void display();
}
class Test implements A, B
{
public void show()
{
System.out.println("Show method");
}
public void display()
{
System.out.println("Display method");
}
}
public class Main
{
public static void main(String[] args)
{
Test t = new Test();
t.show();
t.display();
}
}
Interface and Polymorphism
interface Animal
{
void sound();
}
class Dog implements Animal
{
public void sound()
{
System.out.println("Bark");
}
}
class Cat implements Animal
{
public void sound()
{
System.out.println("Meow");
}
}
public class Main
{
public static void main(String[] args)
{
Animal a;
a = new Dog();
a.sound();
a = new Cat();
a.sound();
}
}
Output
Bark
Meow
Default Methods in Interface (Java 8)
Before Java 8, interfaces could only have abstract methods.
Java 8 introduced default methods.
interface Vehicle
{
default void start()
{
System.out.println("Vehicle Starting");
}
}
class Car implements Vehicle
{
}
public class Main
{
public static void main(String[] args)
{
Car c = new Car();
c.start();
}
}
Static Methods in Interface (Java 8)
interface MathOperations
{
static int square(int x)
{
return x * x;
}
}
public class Main
{
public static void main(String[] args)
{
System.out.println(MathOperations.square(5));
}
}
Output
25
Private Methods in Interface (Java 9)
Java 9 introduced private methods inside interfaces.
interface Test
{
private void message()
{
System.out.println("Private Method");
}
default void show()
{
message();
}
}
Functional Interface
An interface containing only one abstract method is called a Functional Interface.
Used heavily with:
- Lambda Expressions
- Stream API
@FunctionalInterface
interface Demo
{
void display();
}
Functional Interface Example
@FunctionalInterface
interface Calculator
{
int add(int a, int b);
}
public class Main
{
public static void main(String[] args)
{
Calculator c = (a, b) -> a + b;
System.out.println(c.add(10, 20));
}
}
Output
30
Marker Interface
An interface with no methods is called a Marker Interface.
Examples:
- Serializable
- Cloneable
interface Test
{
}
Used to provide special information to JVM.
Difference Between Abstract Class and Interface
| Feature | Interface | Abstract Class |
|---|---|---|
| Multiple Inheritance | Supported | Not Supported |
| Constructors | Not Allowed | Allowed |
| Variables | public static final | Any Type |
| Methods | Abstract, Default, Static | Abstract & Concrete |
| Access Modifiers | Public | Any |
| Keyword | implements | extends |
Real-Time Example of Interface
Payment Gateway System
interface Payment
{
void pay(int amount);
}
class CreditCardPayment implements Payment
{
public void pay(int amount)
{
System.out.println("Paid using Credit Card: " + amount);
}
}
class UpiPayment implements Payment
{
public void pay(int amount)
{
System.out.println("Paid using UPI: " + amount);
}
}
public class Main
{
public static void main(String[] args)
{
Payment p;
p = new CreditCardPayment();
p.pay(5000);
p = new UpiPayment();
p.pay(2000);
}
}
Advantages of Interface
- Achieves abstraction
- Supports multiple inheritance
- Promotes loose coupling
- Improves code maintainability
- Useful for API design
- Encourages standard architecture
Disadvantages of Interface
- Cannot store object state
- Too many interfaces can increase complexity
- Prior to Java 8, interfaces could not contain implementation
Important Interview Questions on Interface
1. Can we create object of interface?
No.
Animal a = new Animal(); // Error
2. Can interface have constructor?
No.
3. Can interface extend another interface?
Yes.
interface A
{
void show();
}
interface B extends A
{
void display();
}
4. Can abstract class implement interface?
Yes.
interface A
{
void show();
}
abstract class Test implements A
{
}
5. Which keyword is used to inherit interface?
implements