An interface provides specifications of what a class should do or not and how it should do. An interface in Java basically has a set of methods that class may or may not apply.
What is Interface in Java?
An interface in Java is a mechanism which we mainly use to achieve abstraction and multiple inheritances in Java.
It also has capabilities to perform a function. The methods in interfaces do not contain any body.
These abstract methods are implemented by classes before accessing them.
An interface provides a set of specifications that other classes must implement.
We can implement multiple Java Interfaces by a Java class. All methods of an interface are implicitly public and abstract. The word abstract means these methods have no method body, only method signature.
An interface can inherit or extend multiple interfaces.
We can implement more than one interface in our class.
Since Java 8, we can have static and default methods in an interface.
Since Java 9, we can also include private methods in an interface.
An interface does not contain any constructors
An interface cannot be extended or inherited by a class; it is implemented by a class.
An interface cannot implement any class or another interface.
An interface is implicitly abstract. While declaring an interface, we do not need to use the keyword abstract.
Each method of an interface is also implicitly abstract, so we need not use the abstract keyword while declaring methods inside an interface.
Each method in an interface is implicitly public.
All variables defined in an interface are public, static, and final. In other words, interfaces can declare only constants, not instance variables.
It is similar like fully abstract class.
It contains all abstract methods only (Till Java 7).
Notes: The Java compiler automatically adds the public and abstract keywords before the methods of an interface. It also adds public, static, and final keywords before the data members.
Relationship between Classes and Interface in Java
In the above figure, we can see that a class can extend another class, a class can implement an interface, and an interface can extend another interface.
When to use Interface in Java?
Consider using an interface in the following cases:
1) When we want to achieve 100% abstraction.
2) If we want to achieve multiple inheritance, that is, implementing more than one interface.
3) When we want to specify the behavior of a particular data type irrespective of who implements its behavior.
When to use Abstract Class in Java?
Consider using abstract classes in the following cases:
1) If we have some related classes that need to share the same lines of code, then we put these classes in abstract classes.
2) If there is a requirement of using access modifiers other than public such as protected and private for methods or fields.
3) When there is a need for defining a state of an object because we need to define a non-static or non-final field.
Difference Between Abstract Class and Interface in Java
Sr No |
Parameter |
Abstract Class |
Interface |
1 |
Keyword |
An abstract keyword is used to create an abstract class. |
An interface keyword is used to create an interface. |
2 |
Type of variables |
Abstract class in Java can have both final, non-final, static, and non-static variables. |
An interface can only have final and static variables that are declared by default. |
3 |
final variables |
An abstract class may or may not have variables declared as final |
In interfaces, variables are by default declared as final. |
4 |
Access Modifiers |
Abstract classes can have all access modifiers: public, protected, private and default. |
No other access modifiers are allowed except the public access modifier. |
5 |
Type of Methods |
An abstract class can have both abstract and non-abstract or concrete methods |
An interface can only have abstract methods. From version 8 of Java, the interface supports static and non-static methods too. |
6 |
Constructors |
An abstract class can have constructors |
An interface cannot have constructors |
7 |
Multiple Inheritance |
Abstract classes do not support Multiple Inheritance. A class can extend only a single abstract class but can implement multiple Java interfaces. |
Interfaces support Multiple Inheritance. |
8 |
Implementation |
We can extend an abstract class using the extends keyword. |
We can implement an interface using the implements keyword. |
9 |
Speed |
Fast |
Slow as it requires extra indirection. |
10 |
When to use |
To avoid independence |
For Future enhancement |
Real Time Examples
I have Account class which implement Saving Account and Current Account interface.
Suppose we have to calculate the area of geometrical shapes, and for each shape, (Like circle, square, Triangle, Rectangle) we have different methods. And all the methods are defined, independent of each other. Only method signatures are written in the Interface.