Is it accurate to say that the "interface" in oop provides a "has a" capability for classes and only used with unrelated classes?

huangapple go评论87阅读模式
英文:

Is it accurate to say that the "interface" in oop provides a "has a" capability for classes and only used with unrelated classes?

问题

  • 在我了解的范围内,接口和抽象类之间的区别是众所周知的话题。我不会在这个问题中重复那些区别。

  • 是否可以说,在面向对象编程中,“接口”提供了类的“具有”能力?

  • “具有”能力是否与“具有”关系相同?

  • 接口只能与不相关的类一起使用吗?

此外,例如在Java中,类ArrayListLinkedList是相关的。为什么要使用接口(List)?已经提到接口用于不相关的类。

如果我理解错了,请指导我。

英文:

I already know the differences between interfaces and abstract classes because it's a well-known topic. I won't repeat those differences in this question. I have another questions. This answer raised a new question in me.

  • Is it accurate to say that the "interface" in oop provides a "has a" capability for classes?

  • Is the capability "has a" the same as the "has a" relation?

  • Are interfaces only used with unrelated classes?

In addition to that, For example in Java the classes ArrayList and LinkedList are related. Why is an interface used? (List) It has been mentioned that interfaces are used for unrelated classes.

If I am mistaken, please guide me accordingly.

答案1

得分: 1

接口定义了类必须实现的方法的契约。它们允许不相关的类共享通用行为并实现多态性。

“拥有关系”表示面向对象编程中的组合,其中一个类包含另一个类的对象。它表示了一种强烈的拥有关系。
“是一个关系”是通过继承建立的。当一个类继承自另一个类(或实现一个接口)时,它被认为是该类或接口的特定类型。

接口用于在不相关的类之间建立通用行为。它们使得可以根据它们共享的接口以统一的方式对待不相关的类。

列表接口示例:在Java中,List接口代表了列表实现的契约。诸如ArrayListLinkedList之类的类实现了List接口,创建了一个“是一个关系”,表示它们满足了契约并且表现为列表。

总结:
接口提供了一种定义不相关类的通用行为的方式,而“拥有关系”表示组合,“是一个关系”表示继承。接口使得不相关的类可以共享通用功能,促进了多态性和代码灵活性。

英文:

Interfaces define a contract of methods that classes must implement. They allow unrelated classes to share common behavior and enable polymorphism.

The "has a" relationship represents composition in object-oriented programming, where one class contains an object of another class. It indicates a strong ownership relationship.
The "is a" relationship is established through inheritance. When a class inherits from another class (or implements an interface), it is considered to be a specific type of that class or interface.

Interfaces are used to establish common behavior among unrelated classes. They enable unrelated classes to be treated uniformly based on their shared interface.

List Interface Example: In Java, the List interface represents a contract for list implementations. Classes like ArrayList and LinkedList implement the List interface, creating an "is a" relationship, indicating they fulfill the contract and behave as lists.

Summary:
interfaces provide a way to define common behavior for unrelated classes, while the "has a" relationship signifies composition, and the "is a" relationship denotes inheritance. Interfaces enable unrelated classes to share common functionality, promoting polymorphism and code flexibility.

答案2

得分: 0

以下是翻译好的部分:

什么是抽象类?

抽象类是一个声明为抽象的类 - 可能包含或不包含抽象方法。抽象类不能被实例化,但可以被子类化。当一个抽象类被子类化时,子类通常提供了其父类中所有抽象方法的实现。

我将稍微解释一下抽象类的概念。与我为什么要使用抽象类的问题有关。有时我只需向一个类添加abstract短语,因为某些方法必须由该类的用户实现,并指定哪些方法必须是抽象的。然而,如果用户使用抽象类,那么其中一些方法已经具有可以被重写的默认实现,并且抽象方法也会重叠,只是语法假定存在abstract修饰符并且不存在方法体,从而仅定义了签名。这是语言的属性,与其他面向对象的语言不同。短语abstract不一定意味着抽象性或这样的解释,而只是指定需要在派生类中重写的方法的可能性。

编程中的抽象是什么?

在面向对象编程中,抽象是三个主要原则之一(与封装和继承一起)。通过抽象过程,程序员隐藏了除了与对象相关的相关数据之外的一切,以减少复杂性并提高效率。

抽象类是否可以没有抽象方法?

是的,我们可以有一个没有抽象方法的抽象类,因为两者是独立的概念。声明一个抽象类意味着它不能被自身实例化,只能被子类化。抽象方法声明意味着该方法将在子类中被定义。

在Java中可以创建抽象类的对象吗?

如果我们可以创建一个抽象类的对象并调用它的没有方法体的方法(因为该方法是纯虚拟的),那么将会导致错误。这就是为什么我们不能创建抽象类的对象。简而言之,具有公共构造函数的抽象类是合法的。抽象类不能被创建,但如果有适当的类实现,它可以被使用。抽象类本身,与继承不同,意味着没有实现代码。如果您使用抽象类,那么您可以限制自己声明方法。接口是所有方法都是抽象方法的抽象类。


以下是回答您的问题:

Q: 接口与“拥有关系”相关联吗?

A: 不,因为它们不能被实例化。

Q: 接口只能与无关的类一起使用吗?

A: 不,它们可以与任何类一起使用。

Q: 为什么在Java中List接口被认为是“是一个”关系,尽管它是一个接口?

A: 实际上它与类没有关系。只有类可以有关系,除了抽象类。

此外,您提到

在Java中,ArrayListLinkedList类是相关的。

但这是绝对错误的,因为这些类是List接口的不同实现。

为什么要使用接口?

List接口是列表集合的抽象。它可能具有不同的实现,但相同的行为。

已经提到接口用于无关的类。

实际上,类是否具有关系并不重要,这里接口是无关的。

英文:

TL;DR

What is an abstract class?

An abstract class is a class that is declared abstract - may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed. When an abstract class is subclassed, the subclass usually provides implementations for all abstract methods in its parent class.

I will decipher the concept of an abstract class a little. In connection with the question why I use an abstract class. Sometimes I just add the phrase abstract to a class, because some methods must be implemented by the user of that class, and specify which methods must be abstract. What is it for, however, if the user uses an abstract class, then some of its methods already have a default implementation that can be overridden, and abstract methods also overlap, only the syntax assumes the presence of the abstract modifier and the absence of the method body, thereby defining only the signature. This is a property of the language and different from other object-oriented languages. The phrase abstract does not necessarily imply abstractness or such an interpretation, but only the possibility of specifying methods that need to be overridden in descendant classes.

What is abstraction in programming?

In object-oriented programming, abstraction is one of the three main principles (along with encapsulation and inheritance). Through the abstraction process, the programmer hides everything but the relevant data about the object in order to reduce complexity and increase efficiency.

Can an abstract class have no abstract methods?

Yes, we can have an abstract class without abstract methods since both are independent concepts. Declaring an abstract class means that it cannot be instantiated by itself and can only be subclassed. An abstract method declaration means that the method will be defined in a subclass.

Can you create an object of an abstract class in Java?

If we could create an object of an abstract class and call its method without a body (because the method is purely virtual), then it would give an error. That's why we can't create an object of an abstract class. In short, it is legal to have a public constructor of an abstract class. An abstract class cannot be created, but it can be used if there is an appropriate implementation of this class. Abstraction itself, unlike inheritance, implies the absence of an implementation code. If you are using an abstract class, then you can limit yourself to declaring methods. Interfaces are abstract classes in which all methods are abstract.


Now to answer your questions:

Q: Are interfaces associated with the "has a" relationship?

A: No, because they can't instantiate.

Q: Are interfaces only used with unrelated classes?

A: No, they are used with any classes.

Q: Why is the interface List in Java considered an "is a" relationship even though it is an interface?

A: Actually it has no relationship with classes. Only classes can have relationships except abstract classes.


In addition to that, you are saying that

>in Java the classes ArrayList and LinkedList are related.

But it's absolutely wrong, because these classes are different implementation of the List interface.

>Why is an interface used?

The interface List is a an abstraction of the list collection. It might have different implementations but the same behaviour.

>It has been mentioned that interfaces are used for unrelated classes.

Actually it doesn't matter if classes have a relationship or not, interfaces are unrelated here.


答案3

得分: 0

如您所知,Java只允许单一继承,但允许您实现尽可能多的接口。您可以将该实现视为一种继承,因为您会覆盖这些方法,类似于抽象类。

在Java 8之前,使用接口意味着必须实现所有方法。自Java 8以来,您现在可以添加默认方法,这不会破坏实现接口的客户端代码,但会扩展接口的功能。

在此了解更多信息。

希望这有所帮助!

英文:

As you know, Java only allows single inheritance but allows you to implement as many interfaces as you want. You can think of that implementation as a form of inheritance because you override those methods similar to an abstract class.

Prior to Java 8, to use an interface meant having to implement all of the methods. Since Java 8 you can now add default methods that won't break client code implementing the interface but that extends the interface functionality.

Learn more here.

Hope this helps!

答案4

得分: 0

在面向对象编程中,“接口”提供了类的“拥有一个”(has a)的能力吗?
不是。表示“拥有一个”关系的字段可以是任何类型,接口,抽象或具体。(你所说的“能力”并不清楚是指什么)

能力“拥有一个”和“拥有一个”关系是相同的吗?
拥有一个(HAS-A)关系是一个类拥有一个特定类型字段与其他属于该字段类型的类之间的关系。我不理解你在“能力”和“关系”之间所划分的区别。

接口只能用于不相关的类吗?
无论我们如何定义“相关”,答案都是否定的。任何类都可以实现一个接口。

为什么在Java中,接口List被认为是一个“是一个”关系,即使它是一个接口?
它并不是。单一类型之间不存在关系。一个“是一个”关系是存在于两种类型之间的,所以一个ArrayList是一个List,一个ArrayList也是一个AbstractList(这是一个抽象类,而不是一个接口),一个RandomAccess和其他接口。

英文:

Is it accurate to say that the "interface" in oop provides a "has a" capability for classes?

No. The field which represents a "has a" relationship can be any type, interface, abstract or concrete. (it's not clear what "capability" means to you)

Is the capability "has a" the same as the "has a" relation?

The HAS-A relation is the relationship between a class which has a field of a type, and other classes which are of the type of that field. I don't understand what distinction you are drawing between "capability" and "relation".

Are interfaces only used with unrelated classes?

However we define "related", the answer is no. Any class can implement an interface.

Why is the interface List in Java considered an "is a" relationship even though it is an interface?

It's not. A single type doesn't have a relationship. An IS-A relationship is between two types, so an ArrayList IS-A List, and an ArrayList also IS-A AbstractList (which is an abstract class, not an interface), a RandomAccess and other interfaces.

huangapple
  • 本文由 发表于 2023年7月23日 18:45:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76747815.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定