简体   繁体   English

为什么可以在抽象类中实现接口,而不能在其他接口中实现?

[英]why interfaces can be implemented in abstract classes but not in other interfaces?

I know that an interface cannot implement in another interface as implementing means writing the body of the methods. 我知道一个接口不能在另一个接口中实现,因为实现意味着编写方法的主体。 This cannot be done in interfaces as none of the methods in the interface will have the body. 这不能在接口中完成,因为接口中的任何方法都没有主体。 {Upto Java 7} {最多Java 7}

But I am confused, if this is the reason why interfaces cannot implement one another then why do the interfaces can be implemented in abstract classes. 但是我很困惑,如果这是接口不能彼此实现的原因,那么为什么接口可以在抽象类中实现。 Since abstract classes cannot define all the methods of the interface necessarily. 由于抽象类不能必然定义接口的所有方法。 So in a way an abstract class is not implementing all of the methods of the interfaces. 因此,以某种方式,抽象类无法实现接口的所有方法。

Interfaces cannot use implements because their purpose is to define interfaces, not provide implementation. 接口不能使用implements因为它们的目的是定义接口而不提供实现。 (They can extend other interfaces, though.) The purpose of classes is to provide implementation, even if only partial. (尽管它们可以extend其他接口。)类的目的是提供实现,即使只是部分实现。

But like almost all rules, the edges are blurry: 但是像几乎所有规则一样,边缘模糊不清:

  • An abstract class doesn't actually have to implement anything. 抽象类实际上不需要实现任何东西。
  • Now that Java interfaces have default methods , in some sense they can implement an interface, just not using the keyword implements : 现在,Java接口具有默认方法 ,从某种意义上讲,它们可以实现接口,而无需使用关键字implements

     interface A { void foo(); } interface B extends A { void bar(); default void foo() { System.out.println("foo"); } } public class Example implements B { public static void main(String[] args) throws Exception { new Example().foo(); } public void bar() { } } 
  • Often, people don't bother to define an interface as distinct from the class, instead using the class both to define the interface to it and provide the implementation of it. 通常,人们不会费心定义与类不同的接口,而是同时使用类来定义其接口提供其实现。 (Some purists have an issue with that, but it's very common practice.) (一些纯粹主义者对此有疑问,但这是非常普遍的做法。)

Fundamentally, though: Interfaces are for defining the interface, and classes are for providing the implementation. 但是,从根本上讲:接口用于定义接口,而类用于提供实现。

First: "This cannot be done in interfaces as none of the methods in the interface will have the body." 第一: “这不能在接口中完成,因为接口中的任何方法都没有主体。” : It's important to note that this is a little out of date. :必须注意,这有点过时了。 Interfaces can have method implementations/bodies (default and/or static methods). 接口可以具有方法实现/主体(默认和/或静态方法)。 This is possible since Java 8. 从Java 8开始这是可能的。

why interfaces can be implemented in abstract classes but not in other interfaces? 为什么可以在抽象类中实现接口,而不能在其他接口中实现?

Your question may be strictly about the implements keyword declaration as that's the only aspect in which it makes a difference. 您的问题可能严格地是关于implements关键字声明的,因为那是它所起作用的唯一方面。 In this sense, it's a question of design. 从这个意义上讲,这是设计问题。 Abstract classes are classes, interfaces are interfaces. 抽象类是类,接口是接口。 There are differences between these two types of component, the most notable of which, in this case, is that concrete classes cannot inherit from multiple abstract classes. 这两种类型的组件之间存在差异,在这种情况下,最值得注意的是具体类不能从多个抽象类继承。 There are very good answers on SO about the differences between abstract classes and interfaces (such as this ). 关于抽象类和接口(例如this )之间的区别,SO上有很好的答案。

Conceptually, though, an interface can extends another interface and then provide an implementation for each of the inherited abstract methods with default methods. 但是,从概念上讲,接口可以extends另一个接口,然后为每个继承的抽象方法提供default方法的实现。 One could argue that this is an implementation of the super-interface. 有人可能会说这是超级接口的一种实现。 But when it comes to the specifics of the language, only a class (abstract or not) can declare to implement an interface. 但是当涉及到语言的细节时,只有一个类(无论是否抽象)都可以声明implement接口。

In the end, though, whether concrete methods are in an interface or in an abstract class, before they're used, an object of a concrete class will have to be created (I'm excluding functional interfaces here), so the difference doesn't matter that much. 最后,尽管具体方法是在接口中还是在抽象类中,但在使用它们之前,必须先创建具体类的对象(此处不包括功能接口),因此区别不在于此。没关系。

Interfaces can extend other interfaces. 接口可以扩展其他接口。 As for abstract classes: they require the implementation of the interfaces methods as soon as they implement it. 至于抽象类:它们要求接口方法在实现后就立即实现。 However the implementation doesn't have to be in the abstract class but only in the classes that extend the abstract class and are not abstract themselves. 但是,实现不必在抽象类中,而仅在扩展抽象类且本身不是抽象的类中。

You have to know something. 你一定要知道 Interfaces Implements Classes and Abstract Class is a Class. 接口实现类 ,抽象类是一个类。 From Java Docs 从Java Docs

Interface can only be implemented by classes or extended by other interfaces. 接口只能由类实现或由其他接口扩展。

So Why Interface can not Implement another Interface? 那么为什么接口不能实现另一个接口?

Because at time you are Implementing to a Class you are defining how you class behave. 因为在您实现类时,您是在定义类的行为。 So Implementing an Interface to another interface will break the purposes of an Interface. 因此,将接口实现为另一个接口将破坏接口的用途。 Implements define that you need an implementation of the methods and the Interface doesn't have implementation only Classes. 实现定义您需要方法的实现,并且接口不只实现类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM