Home  >  Article  >  Java  >  Can java interfaces be inherited?

Can java interfaces be inherited?

青灯夜游
青灯夜游Original
2019-11-18 14:36:527515browse

Can java interfaces be inherited?

#Can Java interfaces be inherited?

Java interfaces can be inherited, and it is multiple inheritance, but only interfaces can inherit interfaces, and classes can only implement interfaces. An interface can inherit another or multiple interfaces, and a common class can implement multiple interfaces.

An interface is a collection of constant values ​​and method definitions. An interface is a special kind of abstract class.

Java classes are single inheritance. classB Extends classA

java interface can be inherited from multiple sources. Interface3 Extends Interface0, Interface1, interface……

The main reason why multiple inheritance of classes is not allowed is that if A inherits B and C at the same time, and B and C have a D method at the same time, how does A decide the method? Which one should you inherit?

But there is no such problem with interfaces. Interfaces are all abstract methods and it doesn't matter who inherits them, so interfaces can inherit multiple interfaces.

Note:

1) If a class implements an interface, it must implement all methods of the interface.

2) The name, return type, and parameters of the method must be exactly the same as those in the interface. If the method's return type is not void, the method body must have at least one return statement.

3) Because the interface method is of public type by default, it must be modified with public when implementing it (otherwise it defaults to protected type, which reduces the scope of use of the method).

Example:

interface A{
    void a1();
}
 
interface B{
    void b1();
}
 
interface C extends A,B{//注意该语法只对接口的继承是合法的
    void c1();
}
 
class D implements C{
 
    @Override
    public void a1() {}
 
    @Override
    public void b1() {}
 
    @Override
    public void c1() {}
}

Interface characteristics:

1. The interface is implicitly abstract. When declaring an interface, there is no need to use the abstract key. Character.

2. Each method in the interface is also implicitly abstract, and the abstract keyword is also not required when declaring it.

3. All methods in the interface are public.

4. When compiling, the public abstract modifier is automatically added to the methods defined in the interface.

5. Member variables in the Java interface can only be jointly modified with public static final, and must be initialized. Value, you don’t need to write public static final, it will be added automatically during compilation.

The above is the detailed content of Can java interfaces be inherited?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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
Previous article:What reflection is java?Next article:What reflection is java?