The interface in Java is a mechanism to achieve abstraction. There can be only abstract methods in the Java interface, not method body. It is used to achieve abstraction and multiple inheritance in Java.
In other words, you can say that interfaces can have abstract methods and variables. It cannot have a method body.
Java Interface also represents the IS-A relationship.
It cannot be instantiated just like the abstract class.
Since Java 8, we can have default and static methods in an interface.
Since Java 9, we can have private methods in an interface.
Why use Java interface?
There are mainly three reasons to use interface. They are given below.
- It is used to achieve abstraction.
- By interface, we can support the functionality of multiple inheritance.
- It can be used to achieve loose coupling.–[TBD]
Internal addition by the compiler
The Java compiler adds public and abstract keywords before the interface method. Moreover, it adds public, static and final keywords before data members.
In other words, Interface fields are public, static and final by default, and the methods are public and abstract.

Implementation: To implement an interface we use the keyword implements
interface Organisation
{
final int total_employees=10;
public void salary();
public void working_hours();
public void uniform();
}
class Staff implements Organisation
{
public static void main (String[] args )
{
Staff robin = new Staff();
robin.salary();
robin.working_hours();
robin.uniform();
System.out.println(total_employees+1);
}
public void salary()
{
System.out.println("robin salary amount is 20000");
}
public void working_hours()
{
System.out.println("8 hours per day");
}
public void uniform()
{
System.out.println("black nd white");
}
}
output
robin salary amount is 20000
8 hours per day
black nd white
11

Multiple interface:
interface Organisation
{
final int total_employees=10;
public void salary();
public void working_hours();
public void uniform();
}
interface Family
{
void go_office();
public void wash_cloth();
public void drop_kids();
}
multiple interfaces:
class Staff2 implements Organisation,Family
{
public static void main (String[] args )
{
Staff2 vinoth = new Staff2();
vinoth.salary();
vinoth.working_hours();
vinoth.uniform();
vinoth.go_office();
vinoth.wash_cloth();
vinoth.drop_kids();
}
public void salary()
{
System.out.println("vinoth salary amount is 40000");
}
public void working_hours()
{
System.out.println("10 hours per day");
}
public void uniform()
{
System.out.println("sky blue nd white");
}
public void go_office()
{
System.out.println("ready to go office");
}
public void wash_cloth()
{
System.out.println("i washed my cloths");
}
public void drop_kids()
{
System.out.println("kids droped at school");
}
}
Output
vinoth salary amount is 40000
10 hours per day
sky blue nd white
ready to go office
i washed my cloths
kids droped at school

Important Points About Interface:
- We can’t create an instance(interface can’t be instantiated) of the interface but we can make the reference of it that refers to the Object of its implementing class.
- A class can implement more than one interface.
- A class that implements the interface must implement all the methods in the interface.
- All the methods are public and abstract. And all the fields are public, static, and final.
- It is used to achieve multiple inheritances.
- It is used to achieve loose coupling.
| S. No. | Class | Interface |
|---|---|---|
| 1. | In class, you can instantiate variables and create an object. | In an interface, you can’t instantiate variables and create an object. |
| 2. | Class can contain concrete(with implementation) methods | The interface cannot contain concrete(with implementation) methods |
| 3. | The access specifiers used with classes are private, protected, and public. | In Interface only one specifier is used- Public. |
References:
Leave a comment