Java Interaces
What is Interface?
Interface is something which helps 2 system to interact with each other, without one system has to
know the details of other.
Or in simple term I can say, it helps to achieve ABSTRACTION.
How to define the interface?
Interface declaration consist of
Modifiers
"Interface keyword"
Interface Name
Comma separated list of parent interfaces
Body
Only Public and Default Modifiers are allowed (Protected and private are not allowed)
public interface Bird {
public void fly();
}
interface Bird {
public void fly();
}
All methods are implicit public only.
Method can not be declared as final.
Interface Implementation:
Overriding method can not have more restrict access specifiers.
Concrete class must override all the methods declared in the interface.
Abstract classes are not forced to override all the methods.
A class can implement from multiple interfaces.
Why we need Interface ?
1. Abstraction:
Using interface, we can achieve full Abstraction means, we can define What class must do, but
not How it will do
Example:
interface Vehicle {
int MAX_VALUE = 100;
String COLOUR = "RED";
void start();
void stop();
}
The Vehicle interface defines what a vehicle should be able to do start(), stop() but not how
it should do it.
This is abstraction: hiding implementation details and only exposing the essential behavior.
class Car implements Vehicle {
@Override
public void start() {
System.out.println("Car has started.");
}
public void stop() {
System.out.println("Car has stopped.");
}
}
The Car class implements the abstract contract (Vehicle interface).
Here, the actual implementation details of start() and stop() are provided.
The abstraction ensures any class that implements Vehicle must provide its own version of
these methods.
Vehicle myCar = new Car();
myCar.start();
myCar.stop();
The reference type is Vehicle (abstract view), not Car.
This means you're interacting with Car through the abstraction of Vehicle.
You don't care which vehicle it is (Car, Bike, Truck...) - you only know it has start() and
stop().
Limitation of abstraction
// myCar.displayFields(); Not allowed
Car myVehicle = (Car) myCar;
myVehicle.displayFields();
Because myCar is declared as type Vechicle, only the abstract methods of Vechicle are
accessible.
To call displayFields() (a Car-specific method, not part of the abstraction), you need to
downcast to Car.
System.out.println(Vehicle.COLOUR);
Constants (MAX_VALUE, COLOUR) are implicitly public static final in interfaces.
They belong to the abstraction, so they can be accessed without creating an object.
2. Polymorphism:
Interface can be used as a Data Type.
we can not create the object of an interface, but it can hold the reference of all the classes
which implements it. And at runtime, it decide which method need to be invoked.
Example :
public interface Bird {
public void fly();
}
Bird is an interface with one abstract method: fly().
It defines a contract: every bird be able to fly.
It does not specify how the flying will happen(implementation is left to classes).
public class Eagle implements Bird {
@Override
public void fly() {
System.out.println("Eagle Fly Implementation");
}
}
public class Hen implements Bird {
@Override
public void fly() {
System.out.println("Hen Fly Implementation");
}
}
Eagle and Hen implement the Bird interface.
Each class provides its own version of fly().
This is method overriding - same method signature, different implementations.
public class Main {
public static void main(String args[]) {
Bird birdObject1 = new Eagle(); // Interface reference → Eagle object
Bird birdObject2 = new Hen(); // Interface reference → Hen object
birdObject1.fly(); // Calls Eagle's fly()
birdObject2.fly(); // Calls Hen's fly()
}
}
3. Multiple Inheritance:
Just like classes, interfaces can inherit from other interfaces using the extends keyword.
Unlike classes:
An interface can extend multiple interfaces (supporting multiple inheritance).
A class can implement multiple interfaes but can extend only one class.
interface A {
void methodA();
}
interface B extends A { // B inherits from A
void methodB();
}
class MyClass implements B {
@Override
public void methodA() {
System.out.println("methodA from interface A");
}
@Override
public void methodB() {
System.out.println("methodB from interface B");
}
}
B extends A, so it inherits methodA() from A.
Any class implementing B must provide implementations for both methodA() and
methodB().
MyClass implements B and therefore must implement both methods.
Multiple Inheritance in Interfaces
Java does not allow multiple inheritance with classes ( to avoid the Diamond problem).
But interfaces do allow multiple inheritance because they only declare abstract methods (no
conflicting state/implementation).
A class implementing such an interface must implement all inherited methods.
interface X {
void showDetails();
void xMethod();
}
interface Y {
void showDetails();
void yMethod();
}
interface Z extends X, Y { // Multiple inheritance (Z inherits from both X and Y)
void showDetails();
void zMethod();
}
class MyClass2 implements Z {
@Override
public void xMethod() { ... }
@Override
public void yMethod() { ... }
@Override
public void zMethod() { ... }
@Override
public void showDetails() { ... }
}
Z extends both X and Y. Both X and Y have showDetails(). This is not an issue because
interfaces only declare methods.
Myclass2 implements Z, so it must implement all methods from X,Y, and Z.
Implementing Multiple Interfaces Drectly
A class can also directly implement multiple interfaces without using an intermediate
interface.
If two interfaces have methods with the same signature (like showDetails() in X and Y), the
class just provides one implementation.
class MyClass3 implements X, Y {
public void xMethod() { ... }
public void yMethod() { ... }
public void showDetails() {
System.out.println("method showDetails common in both interface.");
}
}
MyClass3 directly implements both X and Y.
Since showDetails() exists in both, it only needs one implementation in the class.
4. Nested Interfaces in Java
A nested interface is an interface declared inside a class or another interface.
It is used to logically group interfaces together and to represent a relationship between the
outer class/interface.
Nested interfaces are implicityly static.
2 Types of Nested Interfaces
(A) Interface inside a class
class Outer {
interface NestedInterface {
void nestedMethod();
}
}
class InnerClass implements Outer.NestedInterface {
public void nestedMethod() {
System.out.println("Nested method implementation.");
}
}
NestedInterface is declared inside Outer class.
To implement it, a class must use the syntax Outer.NestedInterface in main().
Outer.NestedInterface obj = new InnerClass();
obj.nestedMethod();
The interface is accessed using the outer class name.
(B) Interface inside another Interface
interface OuterInterface {
interface NestedInterface {
void display();
}
}
class Demo implements OuterInterface.NestedInterface {
public void display() {
System.out.println("Implemented nested interface inside interface.");
}
}
NestedInterface is declared inside OuterInterface.
A class implementing it must use the syntax OuterInterface.NestedInterface in main().
OuterInterface.NestedInterface obj1 = new Demo();
obj1.display();
The nested interface is accessed using the outer interface name.
Key Characteristics
1. Declaration
Inside a class -> className.interfaceName
Inside an interface -> outerInterfaceName.innerInterfaceName
2. Accessibility
Can have any access modifier (public, private, protected, default) if inside a class.
Alyways public if declared inside an interface.
3. Implementation
A class must immplement all methods of the nested interface.
Must use qualified nameing (Outer.NestedInterface).