Java Interface Default and Static Methods
Interface in Java is a concept that is used to achieve abstraction. It contains only abstract methods and does not provide any implementation but In Java 8, Interface is improved by adding default and static methods. Now lets see what is default and static methods.
Default Methods
Methods that are declared using the default keyword inside the interface are known as default methods. These methods are non-abstract methods. The reason behind adding the default method is to allow the developers to add new methods to the interfaces without affecting the classes that implement these interfaces.
Let's see how to declare default methods in the interface.
interface Printable{
// Default method
default void print() {
System.out.println("Printing...");
}
}
We can see the above interface contains a default print() method that contains the body.
We can have any number of default methods and abstract methods in an interface, see how?
interface Printable{
// Default method
default void print() {
System.out.println("Printing...");
}
default void print2D() {
System.out.println("Printing 2D...");
}
// Abstract methods
void print3D();
}
Example: How to Implement Interface having default methods
Let's take an example to understand the implementation of interface that has default methods. Default methods can be called using the implementation class object. Here we have an interface printable that contains two default methods and one abstract method.
interface Printable{
// Default method
default void print() {
System.out.println("Printing...");
}
default void print2D() {
System.out.println("Printing 2D...");
}
// Abstract methods
void print3D();
}
public class Demo implements Printable {
public void print3D() {
System.out.println("Printing 3D...");
}
public static void main(String[] args){
Demo demo = new Demo();
// Calling Default Methods
demo.print();
demo.print2D();
// Calling Abstract Methods
demo.print3D();
}
}
Printing...
Printing 2D...
Printing 3D...
Static Methods
Like default methods interface allows adding static methods to it. We can define static methods inside the interface using the static keyword and are used to define utility methods. Let's see how to declare static methods in interface.
interface Printable{
// Static method
static void print() {
System.out.println("Printing...");
}
}
We can have any number of static methods in the interface. From Java 8, An interface can contain default, static, abstract and non-abstract methods.
Example: Interface having Static Methods
In this example, we have multiple static methods, default methods, and abstract methods. static methods do not inherited into the implementation class so that we have called them using the interface name.
interface Printable{
// Static method
static void print() {
System.out.println("Printing...");
}
static void print2D() {
System.out.println("Printing 2D...");
}
// Default Method
default void print3D() {
System.out.println("Printing 3D...");
}
// Abstract Method
void getInfo();
}
public class Demo implements Printable {
public void getInfo() {
System.out.println("This is new style Java 8 Interface");
}
public static void main(String[] args){
Demo demo = new Demo();
// Calling static Methods
Printable.print();
Printable.print2D();
// Calling default method
demo.print3D();
// Calling Abstract Methods
demo.getInfo();
}
}
Printing...
Printing 2D...
Printing 3D...
This is new style Java 8 Interface