Detailed explanation of default methods and static methods in the interface of Java8 new features

  • 2021-09-11 20:31:24
  • OfStack

Directory 1. Foreword 2. Why use the default method in the Java interface? 3. Why use static methods in the Java interface? 4. Scenario 1: Default Method in Interface 5. Scenario 2: Static Method in Interface 6. Scenario 3: Multiple Inheritance-Use Default Method with Same Name in Two Interfaces 7. References

1. Preface

Java 8 introduces default methods as well as static methods that can be defined in interfaces.

The default method is a normal java Method, but with the default Keyword, the static method is used as usual static Keyword declaration.

2. Why use the default method in the Java interface?

Why java Default methods are introduced in the interface.

Suppose a tractor manufacturer publishes standard interfaces for operating tractors, such as how to shift or stop.

Developers have developed different types of tractors to implement standard tractor interfaces.

If the company adds new functions to its standard interface, such as how to beat tractors?

Developers need to modify their classes to define new methods, which is not a good idea.

Now we need a default method to handle this situation to avoid overriding all the classes that implement the standard tractor interface.

Define the default method in the interface, which will be available in all classes that implement the tractor interface.

3. Why use static methods in the Java interface?

From Java 8 Initially, interfaces can have static methods.

Static methods are associated with classes, not objects. Static methods are used as helper methods.

So if we declare static methods in an interface, we can easily organize our helper methods.

4. Scenario 1: Default method in interface

To understand using the default method, I created 1 interface Village Which has 1 method declaration and 1 default method.

The default method is default Keyword begins.

By default, all methods of the interface are public, so there is no need to use the public Keyword to declare and define methods in an interface.

Village.java


public interface Village {
    void setNumOfPeople(int num);
    void setName(String name);
    default String getBusinessType(){
        return "Most of the Village people do Farming";
    }
}

Create 1 that will implement the Village Interface's Location Class.

The default method is automatically available in this class.

Location.java


public class Location implements Village {
    public int noOfPeople;
    public String name;
    @Override
    public void setNumOfPeople(int n){
        this.noOfPeople = n;
    }
    @Override
    public void setName(String name){
        this.name = name;
    }
}

To test this scenario, create a default0 Class and pass the Location Object accesses the default method.

Main.java


public class Main {
    public static void main(String[] args){
        Location lo = new Location();
        System.out.println(lo.getBusinessType());
    }    
}

Output

Most of the Village people do Farming

5. Scenario 2: Static methods in interfaces

Now we can also write static methods in interfaces. In our Village Interface, I have set the getVillageId() Declared as a static method.

This static method can also be accessed in the default method.


public interface Village {
    void setNumOfPeople(int num);
    void setName(String name);
    static int getVillageId(){
        return 1;
    }
    default String getBusinessType(){
        return "Business type is Farming  and village id:"+getVillageId();
    }
}

I'm right Location Class to use static methods.

We can use static methods by interface name.


public class Location implements Village {
    public int noOfPeople;
    public String name;
    @Override
    public void setNumOfPeople(int n){
        this.noOfPeople = n;
    }
    @Override
    public void setName(String name){
        this.name = name;
    }
    public int getLocationId(){
        return Village.getVillageId();
    }

Main.java


public class Main {
    public static void main(String[] args){
        Location lo = new Location();
        System.out.println(lo.getBusinessType());
        System.out.println("Village id:"+Village.getVillageId());
        System.out.println("Location Id:"+lo.getLocationId());
    }    
}

Output

Business type is Farming and village id:1
Village id:1
Location Id:1

6. Scenario 3: Multiple Inheritance-Use a default method with the same name in both interfaces

In the case of multiple inheritance, where a class implements more than one interface, we need to check how the default methods behave.

Now I am creating a file that contains getBusinessType() The interface of the default method.

City.java


public interface City {
    void setName(String name);
    void setArea(int area);
    default String getBusinessType(){
        return "Service";
    }
}

For multiple inheritance, Location Class will also implement the Village And City Interface.

Due to Village And City Contains default methods with the same name, so due to ambiguity, Location Class forces the default method to be explicitly defined in the class.

Unless we define a method with the same name as the default method, Location Class will not be compiled.

Location.java


public class Location implements Village, City {
    public int noOfPeople;
    public String name;
     public int area;
    @Override
    public void setNumOfPeople(int n){
        this.noOfPeople = n;
    }
    @Override
    public void setName(String name){
        this.name = name;
    }
    @Override
    public void setArea(int area){
        this.area = area; 
    }
    @Override
    public String getBusinessType(){
        return "People do business like Farming and Service.";
    }
    public int getLocationId(){
        return Village.getVillageId();
    }
}

Output

People do business like Farming and Service.
Village id:1
Location Id:1

7. References

"1" Java 8 Default and Static Method in Interface


Related articles: