Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Java 8 Default method with example

In this page we will learn why Java 8 interfaces included Default method, In the earlier post we were discussed Java 7 new feature, now,we will learn why Java 8 added default methods in Java 8. Prior to Java 8 an interface could not define any implementation whatsoever. That means all methods are abstract methods in interface,containing no body. But in JDK 8 has changed this by adding new capability to interface called default method. That means an interface can provide method body by using default method.

Use of Default Method:

Prior to Java 8, you could use abstract and all regular classes to provide  static and default methods. All the methods in interface should be overridden by implementing classes.You can not add new method in an interface without modifying all the implementations. The default method would solve this problem by supplying an implementation if no other implementation is explicitly provided. Thus, the addition of default method will not cause preexisting code to break.

 An interface default method is defined similar to a way a method is defined by a class. The main difference is that the declaration is preceded by a keyword default



Let us look at sample interface as follows: 

Interface sample
{
getSalary();//this is normal interface method declaration

default String getName();//this is a default method for interface in Java 8
{
return "default String";
}

 In the above code sample interface declared two methods namely getSalary() it is a standard interface declaration methods, second methods is getName() which newly added in the java 8,notice that syntax you should declare default keyword before method name and it contains body,so it returns default String. Therefore, it is not necessary for an implementing class to override it.


Example:

interface sample
{
public int getSalary();//this is normal interface method declaration

default String getName()//this is a default method for interface in Java 8
{
return "default String";
}
class sampleImp implements sample
{
public int getSalary()//you need to implement this only in the class
{
return 20000;
}
class Test
{
public static void main(String args[])
{
sampleImp si=new sampleImp();
si.getNumer();//you have to call explicitly since it it is implemented by class sampleImp
si.getString();// you need to call this also because default implementation
}

 
Output:
 20000
default String
 
In the above example, the default implementation of getName() automatically used. It was not necessary to define in sampleImp class. 

If we want different String you can override getName(),it it possible to implementing class to define its own implementation of a default method.

Example:

class sampleImp implements sample
{
public int getSalary()
{
return 20000;
}
public String getName()
{
return "hello java 8";
}

 

 


This post first appeared on Learnprogramingbyluckysir, please read the originial post: here

Share the post

Java 8 Default method with example

×

Subscribe to Learnprogramingbyluckysir

Get updates delivered right to your inbox!

Thank you for your subscription

×