Java 8 Default and Static Method in Interface
April 26, 2014
Java 8 has introduced default method as well as static method that can be defined in interface. Default method is a normal java method but starts with default keyword and static method is declared as usual with static keyword.
Why Default Method in Java Interface?
A question comes in mind why java has introduced default methods in interface. Suppose a tractor manufacturer company has published standard interface to operate the tractor like how to take gear or break etc. Developer has developed different type of tractors implementing the standard tractor interface. What if company adds a new functionality in its standard interface like how to jump the tractor? Developer needs to do changes in their class to define new methods which is not a nice approach. Now we need here default method to handle this situation to avoid rewriting all classes implementing standard tractor interface. Define the default method in interface and it will be available in all classes implementing tractor interface.Why Static Method in Java Interface?
Stating from Java 8, an interface can have static methods. A static method is associated with a class not with the object. Static method works as helper method. So if we declare static methods in interface, it is easy for us to organize our helper methods.Scenario One: Default Method in Interface
To understand using default method, I am creating an interface Village that has some method declarations and one default method. Default method starts with default keyword. By default all methods of interface will be public, so no need to use public keyword to declare and define methods in interface.Village.java
package com.concretepage; public interface Village { void setNumOfPeople(int num); void setName(String name); default String getBusinessType(){ return "Most of the Village people do Farming"; } }
Location.java
package com.concretepage; public class Location implements Village { public int noOfPeople; public String name; @Override public void setNumOfPeople(int n){ this.noOfPeople = n; } @Override public void setName(String name){ this.name = name; } }
Main.java
package com.concretepage; public class Main { public static void main(String[] args){ Location lo = new Location(); System.out.println(lo.getBusinessType()); } }
Most of the Village people do Farming
Scenario Two: Static Method in Interface
Now we can write static method in interface too. In our Village interface, I have declared getVillageId() as an static method. This static method can be accessed in default method as well.Village.java
package com.concretepage; public interface Village { void setNumOfPeople(int num); void setName(String name); static int getVillageId(){ return 1; } default String getBusinessType(){ return "Business type is Farming and village id:"+getVillageId(); } }
Location.java
package com.concretepage; public class Location implements Village { public int noOfPeople; public String name; @Override public void setNumOfPeople(int n){ this.noOfPeople = n; } @Override public void setName(String name){ this.name = name; } public int getLocationId(){ return Village.getVillageId(); } }
Main.java
package com.concretepage; public class Main { public static void main(String[] args){ Location lo = new Location(); System.out.println(lo.getBusinessType()); System.out.println("Village id:"+Village.getVillageId()); System.out.println("Location Id:"+lo.getLocationId()); } }
Business type is Farming and village id:1 Village id:1 Location Id:1
Scenario Three: Multiple Inheritance- Default Method with Same Name in Two Interfaces
In multiple inheritance scenarios, where a class implements more than one interface, we need to check how default method behaves. Now I am creating one more interface that contains getBusinessType() as default method.City.java
package com.concretepage; public interface City { void setName(String name); void setArea(int area); default String getBusinessType(){ return "Service"; } }
Location.java
package com.concretepage; public class Location implements Village, City { public int noOfPeople; public String name; public int area; @Override public void setNumOfPeople(int n){ this.noOfPeople = n; } @Override public void setName(String name){ this.name = name; } @Override public void setArea(int area){ this.area = area; } @Override public String getBusinessType(){ return "People do business like Farming and Service."; } public int getLocationId(){ return Village.getVillageId(); } }
People do business like Farming and Service. Village id:1 Location Id:1