Home > Java > javaTutorial > How to inherit static methods in java interface

How to inherit static methods in java interface

王林
Release: 2023-04-24 14:25:18
forward
1287 people have browsed it

1. Format

public static返回值类型方法名(参数列表){ }
Copy after login

2. Usage Note

Static methods can only be called through the interface name, not through the category Called by name or object name.

Public can be omitted, static cannot be omitted.

3. Example

Since a class can implement multiple interfaces, if the static method in the interface has the same method as before, an inheritance conflict will occur. Thus, interfering with the occurrence of conflicts at the inheritance level. On the other hand, since the fields in the interface can be inherited, there is actually an inheritance conflict for the fields in the interface.

interface TestInterface1 {
    String hello="TestInterface1";
    
}
interface TestInterface1 {
    String hello="TestInterface2";
}
public class Test implements TestInterface1,TestInterface2{
    public static void main(String[] args) {
        System.out.println(Test.hello);//这里会报错
    }
}
Copy after login

The above is the detailed content of How to inherit static methods in java interface. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.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