Home > Java > javaTutorial > The difference between default interface methods and static interface methods in Java 8

The difference between default interface methods and static interface methods in Java 8

王林
Release: 2023-08-30 23:17:17
forward
1232 people have browsed it

Java 8中默认接口方法和静态接口方法的区别

According to Oracle's Javadocs -

The default methods enable you to add new functionality to the library's interfaces and ensure that binaries are compatible with code written for older versions of these interfaces compatibility.

A static method is a method that is associated with the class in which it is defined, not with any object. Each instance of a class shares its static methods.

The static method in the interface is part of the interface. The class cannot implement or override it, but the class can override the default method.

Default method tr>
gentlemen. No Button Static interface method
1

Basic

Static method, only belongs to the interface. We can write the implementation of this method in the interface itself

This is a method with the default keyword, and the class can override this method

2

Method call

Static methods can only be called on interface classes, not on classes .

It can be called on the interface or on the class

3

Method name

Interface and implementation class can have static methods with the same name and will not overwrite each other.

We can override the default method in the implementation class

4.

Use Case

It can be used as a utility method

It can be used to provide common functionality in all implementation classes

Examples of default methods and static methods in interfaces

public interface DefaultStaticExampleInterface {
   default void show() {
      System.out.println("In Java 8- default method - DefaultStaticExampleInterface");
   }
   static void display() {
      System.out.println("In DefaultStaticExampleInterface I");
   }
}
public class DefaultStaticExampleClass implements DefaultStaticExampleInterface {
}
public class Main {
   static void main(String args[]) {
      // Call interface static method on Interface
      DefaultStaticExampleInterface.display();
      DefaultStaticExampleClass defaultStaticExampleClass = new DefaultStaticExampleClass();
     
      // Call default method on Class
      defaultStaticExampleClass.show();
   }
}
Copy after login

The above is the detailed content of The difference between default interface methods and static interface methods in Java 8. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template