Sunday, November 20, 2016

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";
}

 Resolving Default method conflicts:

If a class implements two interfaces,one of which has default method and other default or not with same name and parameter type,by this time you must resolve the conflict. So to understand this we will take an example

Suppose we have an interface Person with  getId() method

public interface Person{
String getName();

default int getId()
{
return 0;
}

Another interface called Dempartment also with same method as getId()

public interface Department{
String getName();
default int getId()
{
return 0;
}

Now what happens if you form a class that implements both of them?

public class Employee implements Person,Department
{
---------
---------
}

In the above example class inherits two getId() methods provided by Person and Department interfaces. Here Java compiler does not choose one over the other. The compiler reports and leaves up to you to resolve ambiguity. So to resolve this conflict, provide a getId() method in the Employee class and either implement your own Id.

Example:

public class Employee implements Person and Department
{
public int getId()
{
return Department.super.getId();
---------------------------
---------------------------
}
}
Note:  In the above example super keyword lets you call super type method. Based on our requirement we should specify which supertype we want.



No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...