Sitemap
Raj Dhok

Raj Dhok

I breath air.

3 min readDec 20, 2025

--

Interface in Java

Press enter or click to view image in full size
  1. Through interface we can achieve 100% abstraction.

example :

interface Fruits{
void color();
void taste();
}
class Apple implements Fruits{
//all methods must be overridden.
public void color(){
System.out.println(“red”);
}
public void taste(){
System.out.println(“sweet”);
}
}
class Main {
public static void main(String[] args) {
Apple a = new Apple();
a.color();
a.taste();
}
}

2. Interface is used to specify our requirements to be implemented.

3. All the methods in an interface are public abstract irrespective of whether we specify or not.

example :

interface Plane{

void disp();

//public abstract void disp(); both have same meaning

}

4. It is compulsory for an implementing class to override all methods of an interface if implementing class is not overriding all the methods of an interface then it must be declared as abstract
example:

interface Fruits{
void color();
void taste();
}
abstract class Apple implements Fruits{
//all methods must be overridden.
public void color(){
System.out.println(“red”);
}
// public void taste(){
// System.out.println(“sweet”);
// }
}
class Main {
public static void main(String[] args) {
//as we can’ create the object/instance of an abstract class
// Apple a = new Apple();
// a.color();
// a.taste();
}
}

5. One interface can have any number of implementing classes.

example :

interface A{}

interface B{}

class C implements A,B{}

6. We cannot create object/instance of an interface.

7. We can create reference of an interface to achieve polymorphism.

example:

interface Vehicle {
void run();
}

class Car implements Vehicle {
public void run() {
System.out.println(“Car is running”);
}
}

class Bike implements Vehicle {
public void run() {
System.out.println(“Bike is running”);
}
}

public class Main{
public static void main(String[] args) {
Vehicle v; // interface reference

Get Raj Dhok’s stories in your inbox

Join Medium for free to get updates from this writer.

v = new Car(); // Car object
v.run(); // Car’s run()

v = new Bike(); // Bike object
v.run(); // Bike’s run()
}
}

8. A class will implement an interface using ‘implements’ keyword.

9. A class can extend another class and implement one or more interface(s) at a same time, in this first the class must extend a class then implement interface(s). (make sure you check the example T-T this one easy it’s just the theory which is hard)

example:

interface A

class AA{}

class BB extends AA implements A{}

class BB extended AA class and also implemented A interface (1st extend then implement).

10. An interface cannot implement another interface however it can extend another interface.

example :

interface A{}

interface B extends A{}

11. In an interface we can have variable but all variables in an interface are public static final by default.

interface Air{

int a = 10;

// same as

//public static final int a = 10;

}

12. We can’t have a constructor in an interface.

13. From Java 8 onward in an interface we can have method with body/implementation however that method must be implemented using default keyword.
example :

interface A {
default void show() {
System.out.println(“Default method”);
}
}

14. Default methods of an interface will get inherited to all implementing classes however it’s not compulsory for implementing classes to override default methods of an interface if the need arises we can override also.

15. We can also have static method interface however static method of an interface must not be abstract it must have implementation and static method of an interface will not get inherited in implementing classes.

16. An implementing class can have specialized methods also however using interface type reference we cannot access directly but can be done through downcasting.

--

--