简体   繁体   English

抽象类和接口类

[英]Abstract and Interface class

Is the below statement correct ?下面的说法正确吗? A extends B is correct if both A and B are either interfaces or classes如果 A 和 B 都是接口或类,则 A extends B 是正确的

How can it be true ?怎么可能是真的? If A is an Interface and B is a class then Interface cannot inherit a class.如果 A 是接口而 B 是类,则接口不能继承类。 Or can we ?或者我们可以吗? If so, can you give some supporting examples please.如果是这样,请您提供一些支持示例。

Depending on context, 'extends' is used for the general notion of 'type X is a subtype of type Y'.根据上下文,“扩展”用于“类型 X 是类型 Y 的子类型”的一般概念。

For example, given:例如,给定:

interface INTERFACE {}

class CLASS implements INTERFACE {}

Then it is certainly correct to say that CLASS implements INTERFACE .那么说CLASS实现了INTERFACE肯定是正确的。

Depending on context, one might say that CLASS extends INTERFACE .根据上下文,人们可能会说CLASS扩展了INTERFACE

For example, INTERFACE x = new CLASS();例如, INTERFACE x = new CLASS(); would be legal, which shows that in the end, CLASS is a subtype of INTERFACE here.将是合法的,这表明最终CLASSINTERFACE的子类型。

As a second example, in generics bounds, the keyword extends is used to indicate subtype relationships, which may be fulfilled with interfaces.作为第二个例子,在泛型边界中,关键字extends用于指示子类型关系,这可以通过接口来实现。 For example, this is real java code:例如,这是真正的 java 代码:

interface INTERFACE {}

class CLASS implements INTERFACE {}

List<? extends INTERFACE> someList;
someList = new ArrayList<CLASS>();

Note, extends is used here.注意,这里使用了extends Had you written List<? implements INTERFACE>你写过List<? implements INTERFACE> List<? implements INTERFACE> , that'd be a compile time error. List<? implements INTERFACE> ,这将是一个编译时错误。

If this is some sort of homework question, the problem with terminology is that there is no Judge Dredd.如果这是某种家庭作业问题,术语的问题在于没有 Dredd 法官。

There's nobody out there with a book of terms in hand who will slap you into next week if you get it wrong.没有人手里拿着一本术语书,如果你弄错了,他会在下周打你一巴掌。 If someone decides to say in casual conversation that, say, ArrayList extends List (note that ArrayList is a class, List is an interface), it's not particularly useful to talk about right and wrong .如果有人决定在闲聊中说,比如说 ArrayList 扩展了 List(注意 ArrayList 是一个类,List 是一个接口),那么谈论并不是特别有用。 The point of opening your mouth and making air pass through your vocal cords is to communicate;张开嘴让空气通过声带的目的是交流; to convey notions and ideas.传达观念和想法。

If the point of the exercise of opening your yap was to convey the notion that ArrayList is a subtype of List and will suffice for any and all places where code needs a List , well, I'd personally say trying to shorten that by saying 'ArrayList extends List' is fair game... - and, insofar as the point is to convey that ArrayList is a subtype, it is entirely correct.如果打开你的 yap 练习的目的是传达这样的概念,即ArrayListList的子类型,并且足以满足代码需要List任何和所有地方,那么,我个人会说试图通过说'来缩短它' ArrayList extends List' 是一个公平的游戏...... - 而且,就这一点而言,要传达 ArrayList 是一个子类型,它是完全正确的。

If you decide to hear 'if I write class ArrayList extends List {} , then it'll compile' - okay, then you have a classic issue of bad communication.如果你决定听到“如果我写class ArrayList extends List {} ,那么它会编译” - 好吧,那么你有一个典型的沟通不良问题。 The talker intended one thing, you understood something else.说话者的意图是一件事,你理解另一件事。 Happens all the time.无时无刻不在发生。 Being more careful with word choices and steering closer to the language spec can help, but as I showed already, java itself uses extends in certain contexts to mean 'subtype of', and in other contexts, specifically it means only 'this is my parent class', and these concepts are not quite the same.更小心地选择单词并更接近语言规范会有所帮助,但正如我已经展示的那样,java本身在某些上下文中使用extends来表示“子类型”,而在其他上下文中,它仅表示“这是我的父级” class',而这些概念并不完全相同。

In this case these are the possible outputs I guess -在这种情况下,我猜这些是可能的输出 -

  1. A extends B (if both are interfaces) A 扩展 B(如果两者都是接口)
  2. A extends B (If both are classes) A 扩展 B(如果两者都是类)

In case of no 1, An interface can extend another interface without any problem.在没有 1 的情况下,一个接口可以毫无问题地扩展另一个接口。

The rest is answered already by @rzwitserloot.其余的已经由@rzwitserloot 回答了。

An interface can extend other interfaces.一个接口可以扩展其他接口。

A class implements more than 1 interface.一个类实现了多个接口。

A class extends only 1 class.一个类只扩展 1 个类。

An interface can't extend a class接口不能扩展类

we use the interface concept in java because if we want to use multiple inheritances in java.我们在java中使用接口概念是因为如果我们想在java中使用多重继承。 Until Java7 we can't write a non-abstract method in the interface but from java8 we can write a non-abstract method using default and static keyword.Java7之前,我们不能在接口中编写非抽象方法,但从java8 开始,我们可以使用defaultstatic关键字编写非抽象方法。

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

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