Java 一个普通的 Class 可以实现多个接口吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21263607/
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-13 07:46:08  来源:igfitidea点击:

Can a normal Class implement multiple interfaces?

javainterfacemultiple-inheritance

提问by Joshua

I know that multiple inheritances between Interfaces is possible, e.g.:

我知道接口之间的多重继承是可能的,例如:

public interface C extends A,B {...} //Where A, B and C are Interfaces

But is it possible to have a regular Class inherit from multiple Interfaces like this:

但是是否可以像这样从多个接口继承常规类:

public class A implements C,D {...} //Where A is a Class and C and D are interfaces

采纳答案by Christian

A Java class can only extend one parent class. Multiple inheritance (extends) is not allowed. Interfaces are not classes, however, and a class can implement more than one interface.

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

The parent interfaces are declared in a comma-separated list, after the implementskeyword.

父接口在implements关键字后以逗号分隔的列表中声明。

In conclusion, yes, it is possible to do:

总之,是的,可以这样做:

public class A implements C,D {...}

回答by AlexR

Yes, it is possible. This is the catch: java does not support multiple inheritance, i.e. class cannot extend more than one class. However class can implement multiple interfaces.

对的,这是可能的。这是一个问题:java 不支持多重继承,即类不能扩展多个类。但是类可以实现多个接口。

回答by Mureinik

In a word - yes. Actually, many classes in the JDK implement multiple interfaces. E.g., ArrayListimplements List, RandomAccess, Cloneable, and Serializable.

一句话—​​—是的。实际上,JDK 中的许多类都实现了多个接口。例如,ArrayList农具ListRandomAccessCloneable,和Serializable

回答by java seeker

public class A implements C,D {...} valid

公共类 A 实现 C,D {...} 有效

this is the way to implement multiple inheritence in java

这是java中实现多重继承的方法

回答by carexcer

Of course... Almost all classes implements several interfaces. On any page of java documentation on Oracle you have a subsection named "All implemented interfaces".

当然......几乎所有的类都实现了几个接口。在 Oracle 上的 Java 文档的任何页面上,您都有一个名为“所有实现的接口”的小节。

Here an exampleof the Dateclass.

这里的示例的的Date类。

回答by Nirmal Dalmia

An interface can extend other interfaces. Also an interface cannot implement any other interface. When it comes to a class, it can extend one other class and implement any number of interfaces.

一个接口可以扩展其他接口。此外,接口不能实现任何其他接口。当涉及到一个类时,它可以扩展另一个类并实现任意数量的接口。

class A extends B implements C,D{...}

回答by NPE

It is true that a java class can implement multiple interfaces at the same time, but there is a catch here. If in a class, you are trying to implement two java interfaces, which contains methods with same signature but diffrent return type, in that case you will get compilation error.

诚然,一个java类可以同时实现多个接口,但这里有一个问题。如果在一个类中,您尝试实现两个 java 接口,其中包含具有相同签名但不同返回类型的方法,在这种情况下,您将收到编译错误。

interface One
{
    int m1();
}
interface Two
{
    float m1();
}
public class MyClass implements One, Two{
    int m1() {}
    float m1() {}
    public static void main(String... args) {

    }
}

output :

输出 :

prog.java:14: error: method m1() is already defined in class MyClass
    public float m1() {}
                 ^
prog.java:11: error: MyClass is not abstract and does not override abstract method m1() in Two
public class MyClass implements One, Two{
       ^
prog.java:13: error: m1() in MyClass cannot implement m1() in Two
    public int m1() {}
               ^
  return type int is not compatible with float
3 errors