Help the world stop coronavirus! Stay home!

Prev Next

Java / Methods

Explain the Default method for interfaces in Java 8.

In Java 8, a new feature "default method implementation for interfaces" is introduced that facilitates backward compatibility for the old interfaces to leverage the lambda expression capability of Java 8 and also existing libraries implementing these interfaces need not have to provide its implementation for the new functionality/method added to the interface.

For example, java.util.List or Collection interface does not have forEach method declaration. Thus, calling such methods will break the collection framework implementations. Java 8 introduces default method so that List/Collection interface can have a default implementation of forEach method, and the class implementing these interfaces need not have to implement the same.

Thus default methods enable you to add new functionality to the interfaces of your libraries and ensure backward compatibility with the codes written using the older versions of those interfaces without having to implement the new functionality.

For creating a default method in Java interface, we need to use default keyword with the method signature.

public interface Fruits {
   default void nature(){
      System.out.println("I am Sweet.");

The default methods are non-static and can be accessed from the Implementing class object as shown in the below example.

public class OrangeClass implements Fruits {

	public static void main(String[] args) {
		Fruits fruit = new OrangeClass();


Java interface default methods are also known as Defender Methods or Virtual extension methods.

A default method cannot override a method from java.lang.Object class as interfaces does not extend Object.

Compilation error occurs when a class tries to implement two or more interfaces having a default method with the same signature.

Other than multiple inheritance, there is no significant difference between an abstract class and an interface in Java 8 by the addition of default method feature.

❤Cash Back At Stores you Love !!!❤

Earn your $10 reward when you make your first purchase through Ebates by signing up with clicking below button.

Ebates Coupons and Cash Back

More Related questions...

Can a main method be declared final? Difference between arguments and parameters in Java. Is the main() method required for all java classes? What is the return type of main() method? Why does the main() method declared static? What is the the argument for main() method? Can a main() method be overloaded? When no command line arguments are passed, does the String array parameter of main() method be null? Can we invoke a static method on a null object reference in Java? Explain the Default method for interfaces in Java 8. Advantages of default method in Interface feature in Java 8. Explain the static interface methods feature in Java 8. Advantages of using static interface method in Java 8. Difference between default and static methods in Java interface. Explain pass by reference and pass by value in Java? Difference between System.exit(0), exit (1) and exit(-1) in Java. What is virtual function/method in Java? Can we define a static method in a Java interface? What is fluent interface in Java? Explain method chaining in Java. What is synthetic method in Java? What is bridge method in Java? What is a method reference in Java 8? Types of method references in Java 8. Are Java static calls less expensive than non-static calls? What is lambda expression in Java 8? What is the target type of lambda expression? Can you Serialize lamda expression in Java8? What is System.out::println expression? Other names for default methods in Java 8. Is abstract modifier allowed in default method? What is the scope of default methods in Java8? How do I extend Interfaces that contain Default Methods? Explain the scenario how Default Method can cause Multiple Inheritance Ambiguity Problem. Why Java doesn't allow overriding of static methods? Explain public static void main(String args[]) method signature. How to call a default method of an interface in a class? How to invoke a static method of an interface?
Show more question and Answers...

Control Statements

Comments & Discussions