Java 8 New Feature 3: Default Methods And Static Methods

Posted by happyme on Thu, 16 Apr 2020 10:00:29 +0200

Attention: Java promotion battalion, the first time the latest articles are delivered, 10T free learning materials are always available!!!

Default Methods

Prior to Java 8, interfaces could only define abstract methods.Implementations of these methods must be provided in separate classes.Therefore, if you want to add new methods to an interface, you must provide its modern code in the class that implements the interface.To overcome this problem, Java 8 introduces the concept of default methods, which allow interfaces to define methods with implementation entities without affecting classes that implement interfaces.

// A simple program to Test Interface default 
// methods in java 
interface TestInterface 
{ 
    // abstract method 
    public void square(int a); 
  
    // default method 
    default void show() 
    { 
      System.out.println("Default Method Executed"); 
    } 
} 
  
class TestClass implements TestInterface 
{ 
    // implementation of square abstract method 
    public void square(int a) 
    { 
        System.out.println(a*a); 
    } 
  
    public static void main(String args[]) 
    { 
        TestClass d = new TestClass(); 
        d.square(4); 
  
        // default method executed 
        d.show(); 
    } 
} 

Output:

 16
 Default Method Executed

Introducing default methods provides backward compatibility so that existing interfaces can be used lambda expressions Instead of implementing these methods in the implementation class.

Static Methods

Interfaces can also define static methods, similar to class static methods.

// A simple Java program to TestClassnstrate static 
// methods in java 
interface TestInterface 
{ 
    // abstract method 
    public void square (int a); 
  
    // static method 
    static void show() 
    { 
        System.out.println("Static Method Executed"); 
    } 
} 
  
class TestClass implements TestInterface 
{ 
    // Implementation of square abstract method 
    public void square (int a) 
    { 
        System.out.println(a*a); 
    } 
  
    public static void main(String args[]) 
    { 
        TestClass d = new TestClass(); 
        d.square(4); 
  
        // Static method executed 
        TestInterface.show(); 
    } 
} 

Output:

 16
 Static Method Executed

Default method and multiple inheritance

If a class implements multiple interfaces and these interfaces contain the same method signature, the implementation class needs to explicitly specify the default method to use, or it should override the default method.

// A simple Java program to demonstrate multiple 
// inheritance through default methods. 
interface TestInterface1 
{ 
    // default method 
    default void show() 
    { 
        System.out.println("Default TestInterface1"); 
    } 
} 
  
interface TestInterface2 
{ 
    // Default method 
    default void show() 
    { 
        System.out.println("Default TestInterface2"); 
    } 
} 
  
// Implementation class code 
class TestClass implements TestInterface1, TestInterface2 
{ 
    // Overriding default show method 
    public void show() 
    { 
        // use super keyword to call the show 
        // method of TestInterface1 interface 
        TestInterface1.super.show(); 
  
        // use super keyword to call the show 
        // method of TestInterface2 interface 
        TestInterface2.super.show(); 
    } 
  
    public static void main(String args[]) 
    { 
        TestClass d = new TestClass(); 
        d.show(); 
    } 
} 

Output:

Default TestInterface1
Default TestInterface2

summary

  1. Starting with Java 8, interfaces can define default methods with implementations.
  2. Interfaces can also define static methods, similar to those in classes.
  3. Introduce default methods to provide backward compatibility with older interfaces so that they can use new methods without affecting existing code.

Topics: Programming Java Lambda