Default method in java 8

You are here : Home / Core Java Tutorials / Java 8 tutorial

1) default method in interface in java 8
Now, we can add add default(non-abstract) method in interfaces in java 8 (Before java 8, we could only write abstract method in interfaces in java 8)

2) But how can we add non-abstract method in interfaces in java 8?
By making method as default in Interface we can make concrete (non-abstract) method in interface. [i.e. we can define method in interface]

3) default method are also called
  • defender methods or
  • extension methods in java 8.  

4) Facts about default method in java 8 >
  • default method are public by default.

5) Full Program/Example to create and use default method of interface in Java 8 >

interface Animals {
  
   /*
   * Define default method in java 8
   */
   default void food() {
       System.out.println("Animal eat food");
   }
}

class Lion implements Animals {
//No no need to override any method of interface (As there is no abstract method in interface)
}

public class MainClass{
   public static void main(String...args) {
          Animals animals = new Lion();
          animals.food();
   }
}
/* OUTPUT
Animal eat food
*/



6) Advantage of default methods in java 8>
There is no need to override default methods of interface in implementing class.
Example - Lion class didn't override default method food.

Before java 8 - Every class implementing interface was needed to override all the methods of interface (As we could only define abstract methods in interface).



7) Can we override default method of interface in java 8?
Yes, we can override default method of interface in java 8.

interface Animals {
   /* Define default method in java 8 */
   default void food() {
       System.out.println("Animal eat food");
   }
}
class Lion implements Animals {
  /*If we want - we can override default method of interface in java 8*/
   @Override
   public void food() {
       System.out.println("Lion eat - flesh");
   }
}
public class MainClass{
   public static void main(String...args) {
          Animals animals = new Lion();
          animals.food();
   }
}
/* OUTPUT
Lion eat - flesh
*/


Override default methods in java 8 - in anonymous inner class >
interface Animals {
   /* Define default method in java 8 */
   default void food() {
       System.out.println("Animal eat food");
   }
}
public class MainClass{
   public static void main(String...args) {
          /*override default methods in java 8 - in anonymous inner class*/
          Animals lion = new Animals() {
                 @Override
          public void food() {
                 System.out.println("Lion eat - flesh");
          }
          };
         
          lion.food();
   }
}
/* OUTPUT
Lion eat - flesh
*/





8) What if two interfaces have declared a default method with same name and a class tries to implement both the interface in java 8 ?
We will face compilation error that Interface1 and Interface2 have same name for default myMethod.  

It is also called DIAMOND problem in java

Exact error shown in eclipse >
"Duplicate default methods named method with the parameters () and () are inherited from the types Interface1 and Interface2"

Solution >
We need to override myMethod in MyClass to avoid this error in java 8.
interface Interface1 {
   default void myMethod() {
       System.out.println("method");
   }
}
interface Interface2 {
   default void myMethod() {
       System.out.println("method");
   }
}
class MyClass implements Interface1, Interface2 {
   public void myMethod() {
       System.out.println("myMethod");
   }
}




Related links >>

Interface in java - Multiple inheritance, Marker interfaces, When to use interface practically, 12 features


Abstract class and abstract methods in java - When to use abstract class or interface practically, 10 features


Labels: Core Java Java 8
eEdit
Must read for you :