Java 为什么一个接口不能实现另一个接口?

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

Why an interface can not implement another interface?

javaoopinheritanceinterface

提问by

What I mean is:

我的意思是:

interface B {...}

interface A extends B {...} // allowed  

interface A implements B {...} // not allowed

I googled it and I found this:

我用谷歌搜索了一下,发现了这个

implementsdenotes defining an implementation for the methods of an interface. However interfaces have no implementation so that's not possible.

implements表示定义接口方法的实现。但是接口没有实现,所以这是不可能的。

However, interface is an 100% abstract class, and an abstract class can implement interfaces (100% abstract class) without implement its methods. What is the problem when it is defining as "interface" ?

但是,接口是100%抽象类,抽象类可以实现接口(100%抽象类)而无需实现其方法。定义为“接口”时有什么问题?

In details,

详细来说,

interface A {
    void methodA();
}

abstract class B implements A {} // we may not implement methodA() but allowed

class C extends B {
   void methodA(){}
} 

interface B implements A {} // not allowed. 
//however, interface B = %100 abstract class B

采纳答案by Jigar Joshi

implementsmeans implementation, when interfaceis meant to declare just to provide interfacenot for implementation.

implements表示实现,wheninterface是为了声明只提供interface不实现。

A 100% abstract classis functionally equivalent to an interfacebut it can also have implementation if you wish (in this case it won't remain 100% abstract), so from the JVM's perspective they are different things.

100%abstract class在功能上等同于 aninterface但如果您愿意,它也可以有实现(在这种情况下它不会保持 100% abstract),因此从 JVM 的角度来看,它们是不同的东西。

Also the member variable in a 100% abstract class can have any access qualifier, where in an interface they are implicitly public static final.

此外,100% 抽象类中的成员变量可以具有任何访问限定符,在接口中它们是隐式的public static final

回答by Colin Hebert

implementsmeans a behaviour will be defined for abstractmethods (except for abstract classes obviously), you define the implementation.

implements意味着将为abstract方法定义行为(显然抽象类除外),您定义实现。

extendsmeans that a behaviour is inherited.

extends意味着行为是继承的。

With interfaces it is possible to say that one interface should have that the same behaviour as another, there is not even an actual implementation. That's why it makes more sense for an interface to extendsanother interface instead of implementing it.

对于接口,可以说一个接口应该与另一个接口具有相同的行为,甚至没有实际的实现。这就是为什么将接口连接到extends另一个接口而不是实现它更有意义的原因。



On a side note, remember that even if an abstractclass can define abstractmethods (the sane way an interface does), it is still a classand still has to be inherited (extended) and not implemented.

附带说明一下,请记住,即使一个abstract类可以定义abstract方法(接口的正常方式),它仍然是一个,仍然必须被继承(扩展)而不是实现。

回答by Landei

Conceptually there are the two "domains" classes and interfaces. Inside these domains you are always extending, only a class implements an interface, which is kind of "crossing the border". So basically "extends" for interfaces mirrors the behavior for classes. At least I think this is the logic behind. It seems than not everybody agrees with this kind of logic (I find it a little bit contrived myself), and in fact there is no technical reason to have two different keywords at all.

从概念上讲,有两个“域”类和接口。在您总是扩展的这些域中,只有一个类实现了一个接口,这有点“跨越边界”。所以基本上接口的“扩展”反映了类的行为。至少我认为这是背后的逻辑。似乎不是每个人都同意这种逻辑(我自己也觉得有点做作),实际上根本没有技术上的理由要使用两个不同的关键字。

回答by Sundeep

Interface is the class that contains an abstract method that cannot create any object.Since Interface cannot create the object and its not a pure class, Its no worth implementing it.

接口是包含不能创建任何对象的抽象方法的类。由于接口不能创建对象并且它不是一个纯类,所以它没有实现它的价值。

回答by JosiahYoder-deactive except..

However, interface is 100% abstract class and abstract class can implements interface(100% abstract class) without implement its methods. What is the problem when it is defining as "interface" ?

但是,接口是 100% 抽象类,抽象类可以在不实现其方法的情况下实现接口(100% 抽象类)。定义为“接口”时有什么问题?

This is simply a matter of convention. The writers of the java language decided that "extends" is the best way to describe this relationship, so that's what we all use.

这只是一个约定问题。Java 语言的作者认为“扩展”是描述这种关系的最佳方式,因此我们都使用它。

In general, even though an interface is "a 100% abstract class," we don't think about them that way. We usually think about interfaces as a promise to implement certain key methods rather than a class to derive from. And so we tend to use different language for interfaces than for classes.

通常,即使接口是“100% 抽象类”,我们也不这么认为。我们通常将接口视为实现某些关键方法的承诺,而不是从中派生的类。所以我们倾向于对接口使用不同的语言而不是类。

As others state, there are good reasons for choosing "extends" over "implements."

正如其他人所说,选择“扩展”而不是“实现”是有充分理由的。

回答by XxANxX

Hope this will help you a little what I have learned in oops (core java) during my college.

希望这对我在大学期间在 oops(核心 Java)中学到的有所帮助。

Implements denotes defining an implementation for the methods of an interface. However interfaces have no implementation so that's not possible. An interface can however extend another interface, which means it can add more methods and inherit its type.

实现表示为接口的方法定义实现。但是接口没有实现,所以这是不可能的。然而,一个接口可以扩展另一个接口,这意味着它可以添加更多方法并继承其类型。

Here is an example below, this is my understanding and what I have learnt in oops.

下面是一个例子,这是我的理解和我在 oops 中学到的东西。

interface ParentInterface{  
        void myMethod();  
}  

interface SubInterface extends ParentInterface{  
        void anotherMethod();  
}  

and keep one thing in a mind one interface can only extend another interface and if you want to define it's function on some class then only a interface in implemented eg below

并记住一件事,一个接口只能扩展另一个接口,如果你想在某个类上定义它的函数,那么只实现一个接口,例如下面

public interface Dog
{
    public boolean Barks();

    public boolean isGoldenRetriever();
}

Now, if a class were to implement this interface, this is what it would look like:

现在,如果一个类要实现这个接口,它会是这样的:

public class SomeClass implements Dog
{
    public boolean Barks{
    // method definition here

    }

    public boolean isGoldenRetriever{
    // method definition here
    }
}

and if a abstract class has some abstract function define and declare and you want to define those function or you can say implement those function then you suppose to extends that class because abstract class can only be extended. here is example below.

如果一个抽象类有一些抽象函数定义和声明,并且你想定义这些函数,或者你可以说实现这些函数,那么你假设扩展该类,因为抽象类只能扩展。下面是示例。

public abstract class MyAbstractClass {

    public abstract void abstractMethod();
}

Here is an example subclass of MyAbstractClass:

这是 MyAbstractClass 的示例子类:

public class MySubClass extends MyAbstractClass {

    public void abstractMethod() {
        System.out.println("My method implementation");
    }
}

回答by Sahil

Interface is like an abstraction that is not providing any functionality. Hence It does not 'implement' but extend the other abstractions or interfaces.

接口就像一个不提供任何功能的抽象。因此,它不会“实现”而是扩展其他抽象或接口。