一个接口可以在 Java 中扩展多个接口吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19546357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:15:36  来源:igfitidea点击:

Can an interface extend multiple interfaces in Java?

javainheritancemultiple-inheritanceextends

提问by Prateek

Can an interface extend multiple interfaces in Java? This code appears valid in my IDE and it does compile:

一个接口可以在 Java 中扩展多个接口吗?此代码在我的 IDE 中显示有效,并且可以编译:

interface Foo extends Runnable, Set, Comparator<String> { }

but I had heard that multiple inheritance was not allowed in Java. Why does there appear to be an exception for interfaces?

但我听说 Java 中不允许多重继承。为什么接口会出现异常?

采纳答案by Suresh Atta

Yes, you can do it. An interface can extend multiple interfaces, as shown here:

是的,你可以做到。一个接口可以扩展多个接口,如下所示:

interface Maininterface extends inter1, inter2, inter3 {  
  // methods
}

A single class can also implement multiple interfaces. What if two interfaces have a method defining the same name and signature?

单个类也可以实现多个接口。如果两个接口有一个定义相同名称和签名的方法怎么办?

There is a tricky point:

有一个棘手的问题:

interface A {
    void test();
}

interface B {
    void test();
}

class C implements A, B {

    @Override
    public void test() {

    }     

}

Then single implementation works for both :).

然后单一实现适用于两者:)。

Read my complete post here:

在这里阅读我的完整帖子:

http://codeinventions.blogspot.com/2014/07/can-interface-extend-multiple.html

http://codeinventions.blogspot.com/2014/07/can-interface-extend-multiple.html

回答by Masudul

Can an interface extend multiple interfaces in java?

一个接口可以在java中扩展多个接口吗?

Answer is: Yes.

答案是:是的。

According to JLS

根据JLS

An interface may be declared to be a direct extension of one or more other interfaces, meaning that it implicitly specifies all the member types, abstract methods, and constants of the interfaces it extends, except for any member types and constants that it may hide.

一个接口可以被声明为一个或多个其他接口的直接扩展,这意味着它隐式指定了它扩展的接口的所有成员类型、抽象方法和常量,除了它可能隐藏的任何成员类型和常量。

回答by But I'm Not A Wrapper Class

An interfacecan extend multiple interfaces.

一个接口可以扩展多个接口

A classcan implement multiple interfaces.

一个可以实现多个接口

However, a classcan only extend a single class.

但是,一个只能扩展一个类

Careful how you use the words extendsand implementswhen talking about interfaceand class.

小心你如何使用这些词extends以及implements在谈论interface和时class

回答by AlexWien

You can extend multiple Interfacesbut you cannot extend multiple classes.

您可以扩展多个,Interfaces但不能扩展多个classes

The reason that it is not possible in Java to extending multiple classes, is the bad experience from C++ where this is possible.

在 Java 中无法扩展多个类的原因是 C++ 的糟糕体验,这是可能的。

The alternative for multipe inheritance is that a class can implement multiple interfaces (or an Interface can extend multiple Interfaces)

多重继承的替代方案是一个类可以实现多个接口(或者一个接口可以扩展多个接口)

回答by Cem Sultan

I think your confusion lies with multiple inheritance, in which it is bad practise to do so and in Java this is also not possible. However, implementing multiple interfaces is allowed in Java and it is also safe.

我认为您的困惑在于多重继承,这样做是不好的做法,而在 Java 中这也是不可能的。但是,Java 允许实现多个接口,而且它也是安全的。

回答by Hoang Ong

A Java class can only extend one parent class. Multiple inheritance is not allowed. Interfaces are not classes, however, and an interface can extend more than one parent interface.

一个 Java 类只能扩展一个父类。不允许多重继承。然而,接口不是类,一个接口可以扩展多个父接口。

for example, take a look here: http://www.tutorialspoint.com/java/java_interfaces.htm

例如,看看这里:http: //www.tutorialspoint.com/java/java_interfaces.htm

回答by shankar upadhyay

From the Oracle documentation page about multiple inheritance type,we can find the accurate answer here. Here we should first know the type of multiple inheritance in java:-

从 Oracle 文档关于多继承类型的页面,我们可以在这里找到准确的答案。这里首先要知道java中多重继承的类型:-

  1. Multiple inheritance of state.
  2. Multiple inheritance of implementation.
  3. Multiple inheritance of type.
  1. 状态的多重继承。
  2. 实现的多重继承。
  3. 类型的多重继承。

Java "doesn't support the multiple inheritance of state, but it support multiple inheritance of implementation with default methods since java 8 release and multiple inheritance of type with interfaces.

Java“不支持状态的多重继承,但它支持自Java 8发布以来默认方法实现的多重继承和接口类型的多重继承。

Then here the question arises for "diamond problem" and how Java deal with that:-

那么这里出现了“钻石问题”以及Java如何处理这个问题:-

  1. In case of multiple inheritance of implementation java compiler gives compilation error and asks the user to fix it by specifying the interface name. Example here:-

                interface A {
                    void method();
                }
    
                interface B extends A {
                    @Override
                    default void method() {
                        System.out.println("B");
                    }
                }
    
                interface C extends A { 
                    @Override
                    default void method() {
                        System.out.println("C");
                    }
                }
    
                interface D extends B, C {
    
                }
    
  1. 在实现的多重继承的情况下,java 编译器会给出编译错误并要求用户通过指定接口名称来修复它。这里的例子:-

                interface A {
                    void method();
                }
    
                interface B extends A {
                    @Override
                    default void method() {
                        System.out.println("B");
                    }
                }
    
                interface C extends A { 
                    @Override
                    default void method() {
                        System.out.println("C");
                    }
                }
    
                interface D extends B, C {
    
                }
    

So here we will get error as:- interface D inherits unrelated defaults for method() from types B and C interface D extends B, C

所以在这里我们会得到错误:-接口 D 从类型 B 和 C 中继承了与 method() 无关的默认值 接口 D 扩展了 B,C

You can fix it like:-

你可以像这样修复它:-

interface D extends B, C {
                @Override
                default void method() {
                    B.super.method();
                }
            }
  1. In multiple inheritance of type java allows it because interface doesn't contain mutable fields and only one implementation will belong to the class so java doesn't give any issue and it allows you to do so.
  1. 在 java 类型的多重继承中允许它,因为接口不包含可变字段,并且只有一个实现将属于该类,因此 java 不会产生任何问题,它允许您这样做。

In Conclusion we can say that java doesn't supportmultiple inheritance of statebut it does supportmultiple inheritance of implementation and multiple inheritance of type.

总之,我们可以说java不支持状态的多重继承,但它支持实现的多重继承和类型的多重继承