In this tutorial, you’ll dive deep into the concept of Interface in Java, a powerful feature that enables developers to achieve complete abstraction in Java. An interface defines what a class must do, but leaves the details of how it should be done to the implementing class. This makes interfaces a cornerstone of clean, modular, and flexible Java programming
How does Interface differ from a class
Although an Interface in Java looks syntactically similar to a class, it differs in several important ways:
- Interfaces cannot have instance variables, meaning they don’t maintain state.
- Methods inside an interface are declared without a body and end with a semicolon.
- An interface cannot be instantiated, so constructors are not allowed.
- A class implements an interface rather than extending it.
- An interface itself can extend multiple interfaces, supporting multiple inheritance in Java.
Please note that, Java 8 onward, it is possible to add a default implementation to a method in an interface as well as interface static methods and even Private Methods in Java Interface Java 9 onward, here we'll discuss interfaces in its normal form.
General form of interface in Java
access_modifier interface name {
type final_var1 = value;
type final_var2 = value;
----
----
return_type method_name1(param_list);
return_type method_name2(param_list);
----
----
}
When no access modifier is specified, an Interface in Java has default access, meaning it is visible only to other members within the same package. However, when an interface is declared as public, it becomes universally accessible and can be used by any program across different packages.
For top level interfaces only these two access modifiers (default and public) are permitted, there are nested interfaces which can be declared as public, private or protected. We'll discuss nested interfaces later in the post.
All variables declared inside an Interface in Java are implicitly public, static, and final. This means they act as constants: they must be initialized at the time of declaration and cannot be modified by the implementing class. Such behavior ensures consistency and prevents accidental changes across different implementations.
Similarly, all methods within an Interface in Java are implicitly abstract and public. These methods define the contract that any implementing class must fulfill, but they do not provide the actual implementation. By enforcing this rule, interfaces promote clean design, flexibility, and a clear separation of responsibilities in Java applications
Note that Java 9 onward you can also add private methods to a Java interface. Please refer Private Methods in Java Interface to know more about adding private methods in a Java interface.
Java Interface example
public interface MyInterface {
int i = 10;
Number method1();
void method2(String Id);
}
Here is an interface MyInterface which has one variable i, which is public static final implicitly. There are 2 methods, one has no return type, one returns Number.
Implementing an Interface in Java
In Java, class uses implements keyword to implement an interface.
public class MyClass implements MyInterface {
public Integer method1() {
System.out.println("in method 1" + i);
return null;
}
public void method2(String Id) {
System.out.println("in method 2");
}
public static void main(String[] args) {
}
}
When implementing methods defined in interfaces there are several important points-
- A class can implement more than one interface, in that case interfaces are separated with a comma.
class class_name implements interface 1, interface 2{ } - The methods of an interface that are implemented by a class must be declared public i.e. visibility can't be reduced.
- The signature of the interface method must remain same while implementing it. In case of return type subclass of the return type as defined in interface can also be used. As in the above program method1 has the return type Number where as in the implemented method in the class has the return type Integer. It is permissible as Integer is the sub-class of Number.
- The initialized variable in the interface is constant and it is not allowed to change that value in the implementing class. For example in the above program there is a variable i with value 10 if we try to change that value to 20 in the implementing class it will throw compiler error "The final field MyInterface.i cannot be assigned".
- Any number of classes can implement an interface and each class is free to provide their own implementation. That's how using interfaces, Java fully utilizes "one interface, multiple methods" aspect of polymorphism.
- A class can extend only one class, but a class can implement many interfaces. Which means multiple inheritance is not allowed in Java
- An interface can extend another interface, similar to the way that a class can extend another class.
Extending an interface in Java
Just like class, if an interface in Java is inheriting from another interface it uses extends keyword.
An interface, unlike class, can extend more than one interface. If a class implements an interface that
inherits another interface, it must provide implementation for all the methods that are there in the inheritance
chain of interface.
Extending an interface Java Example
// Interface
public interface MyInterface {
int i = 10;
Number method1();
void method2(String Id);
}
// extending interface
interface B extends MyInterface{
void method3();
}
// class implements all methods of MyInterface and B
public class MyClass implements B {
public Integer method1() {
System.out.println("in method 1" + i);
return null;
}
public void method2(String Id) {
System.out.println("in method 2");
}
public void method3() {
System.out.println("in method 3");
}
public static void main(String[] args) {
}
}
It can be seen that the class Myclass has to implement all the methods in the inheritance chain of the interface.
Partial implementation of interface by a class
If a class implements an interface but does not implement all the methods of that interface then that class must be declared as abstract.
public interface MyInterface {
void method1();
String method2(String Id);
}
Compiler error that class must implement methods declared in MyInterface interface.
But we can declare the class as abstract in that case
public abstract class AbstractClassDemo implements MyInterface {
public static void main(String[] args) {
System.out.println();
}
}
Nested Interfaces in Java
An interface or a class can have another interface. Such an interface is known as nested interface or a member interface in Java.
A nested interface can be declared as public, private or protected. When a nested interface is used outside,
it must be used as a fully qualified name i.e. must be qualified by the name of the class or interface of which
it is a member.
Java nested interface Example
// Class with nested interface
class A{
public interface TestInterface{
void displayValue(String value);
}
}
// class implementing the nested interface
class B implements A.TestInterface{
public void displayValue(String value) {
System.out.println("Value is " + value);
}
}
public class MyClass{
public static void main(String[] args) {
// reference of class B assigned to nested interface
A.TestInterface obRef = new B();
obRef.displayValue("hello");
}
}
Output
Value is hello
Interface and run time polymorphism
As we already know that there can't be an object of an interface, but interface can be used to create object references. As run time polymorphism in Java is implemented through the use of super class reference thus interface can be used to provide super class reference which holds references of sub-classes at run time and provide the appropriate functionality.
Run time polymorphism using interface Java Example
Let's assume that in an application there is a need to handle payment done through several modes like;
cash, cheque, credit card etc. and based on the mode of the payment the functionality may be different.
This can be achieved through an interface where the interface defines a method payment and then several classes
implement that interface and provide the functionality for the payment method according to the business needs.
That's how using interfaces, Java fully utilizes "one interface, multiple methods" aspect of polymorphism.
public interface PaymentInt {
public void payment(double amount);
}
// Cash Payment implementation of Payment interface
class CashPayment implements PaymentInt{
// method implementation according to cash payment functionality
public void payment(double amount) {
System.out.println("Cash Payment of amount " + amount);
}
}
//Cheque Payment implementation of Payment interface
class ChequePayment implements PaymentInt{
// method implementation according to cheque payment functionality
public void payment(double amount) {
System.out.println("Cheque Payment of amount " + amount);
}
}
//CreditCard Payment implementation of Payment interface
class CreditCardPayment implements PaymentInt{
// method implementation according to credit card payment functionality
public void payment(double amount) {
System.out.println("CreditCard Payment of amount " + amount);
}
}
public class PaymentDemo {
public static void main(String[] args) {
// Payment interface reference holding the CashPayment obj
PaymentInt paymentInt = new CashPayment();
paymentInt.payment(134.67);
// Payment interface reference holding the CreditCardPayment obj
paymentInt = new CreditCardPayment();
paymentInt.payment(2347.89);
// Payment interface reference holding the ChequePayment obj
paymentInt = new ChequePayment();
paymentInt.payment(1567.45);
}
}
Output
Cash Payment of amount 134.67 CreditCard Payment of amount 2347.89 Cheque Payment of amount 1567.45
It can be seen how at run time reference is changed and the appropriate payment method is called.
Points to note-
- Interfaces help in achieving full abstraction in Java.
- For top level interfaces only default and public access modifiers are permitted.
- All the variables in interface are implicitly public, static and final.
- All the methods in an interface are implicitly public and abstract.
- The methods of an interface that are implemented by a class must be declared public.
- If a class implements an interface but does not implement all the methods of that interface then that class must be declared as abstract.
- A class can extend only one class, but implement many interfaces.
- An interface, unlike class, can extend more than one interface.
- An interface or a class can have another interface. Such an interface is known as nested interface or a member interface.
- Any number of classes can implement an interface and each class is free to provide their own implementation. That's how using interfaces, Java fully utilizes "one interface, multiple methods" aspect of polymorphism.
- With Java 8, it is possible to add a default implementation to a method in an interface.
That's all for this topic Interface in Java With Examples. If you have any doubt or any suggestions to make please drop a comment. Thanks!
>>>Return to Java Basics Tutorial Page
Related Topics
You may also like-







