Before java 8 an interface can have only constant variables and abstract methods. Java 8 and 9 introduced some new features
in interfaces like default methods, static methods
and private methods. Let's see these features of interfaces one by one.
Interface changes in Java 8
Java 8 has introduced two new features in interfaces, default methods and static
methods. From java 8 onward an interface can have following components inside it.
- Constant variables
- Abstract methods
- Nested Types
- Default methods
- Static methods
Default methods in java interfaces
From java 8 onward an interface can have default method with a body. A default method is defined using
default keyword. The syntax of declaring a default method is :
interfaceinterfaceName {defaultreturn_type methodName() {// method definition} } Example:interfaceMyInterface {default voidprint() {// method definition} }
Some key points about default methods are :
- A default method must have a body.
- The access modifier of default methods are implicitly
public. - The class implementing such interface are not required to implement default methods. If needed, implementing class can override default methods.
Default methods enable you to add new functionality to the interfaces of your application and ensures that your old version of code won't break as it doesn't force the classes to implement this. Before java 8 adding new method in an interface broke the classes which already had implemented that interface.
The purpose of providing this feature is to allow the developers to add new functionalities to their interfaces whenever needed, without breaking their older version of code.
Java program of default methods in interface
interfaceMyInterface {voidcalculateArea();default voidprint() { System.out.println("This is default method"); } }classDefaultMethodimplementsMyInterface {public voidcalculateArea() { System.out.println("Calculate area in this method"); }public static voidmain(String args []) { DefaultMethod obj =newDefaultMethod(); obj.print(); obj.calculateArea(); } }
Output:
This is default method
Calculate area in this method
Static methods in java interfaces
From java 8 onward you can define static methods in an interface which can be called without the
object of implementing class. The syntax of declaring static method is :
interfaceinterfaceName {staticreturn_type methodName() {// method definition} } Example:interfaceMyInterface {static voidprint() {// method definition} }
Some key points about static methods are :
- A static method must have a body.
- The access modifier of static methods are implicitly
public. - These method must be called using interface name.
- Since these methods are
static, we cannot override them in implementing class.
Java program of static method in interface
interfaceMyInterface {voidcalculateArea();static voidprint() { System.out.println("This is static method"); } }classStaticMethodimplementsMyInterface {public voidcalculateArea() { System.out.println("Calculate area in this method"); }public static voidmain(String args []) { StaticMethod obj =newStaticMethod(); obj.calculateArea(); MyInterface.print(); } }
Output:
Calculate area in this method
This is static method
Interfaces changes in Java 9
Java 9 introduced private methods and private static
methods in interfaces. Static methods in java 8 can not be private but from java 9 it can be. From java 9 onward an interface can have following components inside it.
- Constant variables
- Abstract methods
- Nested Types
- Default methods
- Static methods
- Private methods
- Private Static methods
Private and Private Static Methods in Java Interfaces
A private method is defined using private access modifier. The basic syntax of declaring a
private and private static method in an interface is :
interfaceinterfaceName {privatereturn_type methodName() {// method definition}private staticreturn_type methodName() {// method definition} } Example:interfaceMyInterface {private voidmessage() {// method definition}private static voiddetail() {// method definition} }
Some key points about private methods are :
- A private method must have a body.
- These method can be accessed within the interface only.
- Since these methods are private, we cannot override them in implementing class.
The purpose of adding private methods in interfaces is to improve the code re-usability. These methods are generally used as utility methods which helps other defined methods inside that interface.
Java program of private method in interface
interfaceMyInterface {default voidprint() { System.out.println("This is default method");// calling private methodsmessage(); detail(); }private voidmessage() { System.out.println("This is private method"); }private static voiddetail() { System.out.println("This is private static method"); } }classPrivateMethodsimplementsMyInterface {public static voidmain(String args []) { PrivateMethods obj =newPrivateMethods(); obj.print(); } }
Output:
This is default method
This is private method
This is private static method
- An interface can have multiple default methods from java 8 onward.
- An interface can have multiple private and static methods.
- If an interface extends another interface, private methods are not inherited.
- From java 8 onward we can define main method inside an interface and we can run it like classes.


