JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Java 8 Interface Default Methods


In the previous post we have seen Lambda Expressions. In this post we will see another java 8 new feature "Default Methods" in interfaces. Before Java 8, the interface only contains method signatures. With Java 8 new feature Default Methods or Defender Methods, you can include method body within the interface.

Lets start with an example:

package com.java2novice.dm;

public interface FirstInterface {

	default void someMethod(){
		System.out.println("from FirstInterface...");
	}
}

Wait, what is default here? This is the new way of declaring the method body in Java 8 for an interface. As seen above, default methods comes along with implementation. You dont need to implement default methods if your class implements above interface, below is an example:

package com.java2novice.dm;

public class TestMe implements FirstInterface {

	public static void main(String[] args) {
		new TestMe().someMethod();
	}
}

We can also override the default methods, but you dont need to specify default in the method signature.

package com.java2novice.dm;

public class TestMe implements FirstInterface {

	@Override
	public void someMethod(){
		System.out.println("from TestMe class...");
	}
}

Why do we need to implement a method within the interface?

Let's say you have an interface which has multiple methods, and multiple classes are implementing this interface. One of the method implementation can be common across the class, we can make that method as a default method, so that the implementation is common for all classes.

How to work with existing interfaces?

Second scenario where you have already existing application, for a new requirements we have to add a method to the existing interface. If we add new method then we need to implement it through out the implementation classes. By using the Java 8 default method we can add a default implementation of that method which resolves the problem.

When working with multiple inheritance:

If we have two interfaces, one with default method and another with just method signature (normal way of defining method in the interfaces).

public interface FirstInterface {
	default void someMethod(){
		System.out.println("Am from interface");
	}
}

public interface SecondInterface {
	void someMethod();
}

If any class implements two of the above interfaces, then someMethod() method became abstract method. We have to implement someMethod() method with in the implementation class.

If we have two interfaces, both are having same default method with different implementation, as shown below:

public interface FirstInterface {
	default void someMethod(){
		System.out.println("Am from first interface");
	}
}

public interface SecondInterface {
	default void someMethod(){
		System.out.println("Am from second interface");
	}
}

If any class implements above two interfaces, there will be an ambiguity. So we have to implement someMethod() method.

If we want to call the perticular default method implementation then you can call like <InterfaceName>.super.<method-name>() as shown below:

public class TestMe implements FirstInterface,SecondInterface {
       	
	public void someMethod() {
		FirstInterface.super.someMethod();
	}
}

If we have two interfaces, both are having same default method with different implementation. But one interface extends other as shown below:

public interface FirstInterface {
	default void someMethod(){
		System.out.println("Am from first interface");
	}
}

public interface SecondInterface extends FirstInterface{
	default void someMethod(){
		System.out.println("Am 2 from interface");
	}
}

Here SecondInterface overrides the someMethod() method from FirstInterface.

What is the difference between abstract class and interface?

Abstract class can have constructor, where you need an object to call the methods in subclass. But in case of default method without any reference you can invoke the interface method, like InterfaceName.super.method()

<< Previous Program 
Knowledge Centre
What is System.out in Java
In System.out, out is an instance of PrintStream. It is a static member variable in System class. This is called standard output stream, connected to console.
Famous Quotations
I respect faith, but doubt is what gets you an education.
-- Wilson Mizner

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2017 by Nataraja Gootooru. All Rights Reserved.