英文:
Is an abstract class without any implementation and variables effectively interface?
问题
我正在复习面向对象编程(OOP)的概念,阅读《设计模式:可复用面向对象软件的元素》。
在这本书中,它对接口进行了如下定义:
由对象操作定义的所有签名的集合被称为对象的接口。(第39页)
而对抽象类的定义如下:
抽象类是其主要目的是为其子类定义通用接口的类。抽象类将其部分或全部的实现推迟到子类中定义的操作;因此,抽象类不能被实例化。抽象类声明但未实现的操作称为抽象操作。未是抽象的类称为具体类。(第43页)
我疑惑的是,如果我定义了一个没有任何内部数据(变量)和具体操作的抽象类,只有一些抽象操作,那它岂不是有效地只是一组签名?那岂不是就是一个接口?
所以这是我的第一个问题:
- 我可以说,一个只包含抽象函数的抽象类在“实际上(或从理论上)”是一个接口吗?
然后我想到,这本书还提到了类型和类的概念。
对象的类定义了对象的实现方式。类定义了对象的内部状态和操作的实现。相比之下,对象的类型仅指其接口 - 即其可以响应的请求集合。一个对象可以有多个类型,不同类的对象可以具有相同的类型。(第44页)
然后我想起一些语言,比如Java,不允许多重继承,但允许多重实现。因此我猜想对于一些语言(如Java),仅具有抽象操作的抽象类并不等同于接口。
所以这是我的第二个问题:
- 我可以说,在支持多重继承的语言中,仅具有抽象函数的抽象类“通常等同于”接口吗?
我的第一个问题类似于检查定义,而第二个问题涉及其他语言的工作方式。我主要使用Java和Kotlin,所以对于其他支持多重继承的语言并不太确定。我不需要对当前面向对象编程语言进行全面的综述,但是对单个语言(例如Python)的一点提示将非常有帮助。
英文:
I'm reviewing the concepts of OOP, reading <Design Patterns: Elements of Reusable Object-Oriented Software>.
Here the book defines interface as
> The set of all signatures defined by an object’s operations is called the interface to the object. (p.39)
And the abstract class as
> An abstract class is one whose main purpose is to define a common interface for its subclasses. An abstract class will defer some or all of its implementation to operations defined in subclasses; hence an abstract class cannot be instantiated. The operations that an abstract class declares but doesn’t implement are called abstract operations. Classes that aren’t abstract are called concrete classes. (p.43)
And I wonder, if I define an abstract class without any internal data (variables) and concrete operations, just some abstract operations, isn't it effectively just a set of signatures? Isn't it then just an interface?
So this is my first question:
- Can I say an abstract class with only abstract functions is "effectively (or theoretically)" an interface?
Then I thought, the book also says something about types and classes.
> An object’s class defines how the object is implemented. The class defines the object’s internal state and the implementation of its operations. In contrast, an object’s type only refers to its interface—the set of requests to which it can respond. An object can have many types, and objects of different classes can have the same type. (p.44)
Then I remembered that some languages, like Java, does not allow multiple inheritance while it allows multiple implementation. So I guess for some languages (like Java), abstract class with only abstract operations != interfaces.
So this is my second question:
- Can I say an abstract class with only abstract functions is "generally equivalent to" an interface in languages that support multiple inheritance?
My first question was like checking definitions, and the second one is about how other languages work. I mainly use Java and Kotlin so I'm not so sure about other languages that support multiple inheritance. I do not expect a general, comprehensive review on current OOP languages, but just a little hint on single language (maybe python?) will be very helpful.
答案1
得分: 2
- 不是的。
在Java中,每个类都是Object
类的子类,因此你不能只有抽象方法的抽象类。它总是会继承自Object
的方法实现:hashCode()
,equals()
,toString()
等。
- 是的,基本上是这样的。
在C++中,例如,没有专门的interface
关键字,接口只是一个没有实现的类。在C++中没有通用的基类,所以你真的可以创建一个没有实现的类。
多重继承并不是真正的决定性特点。Java 具有某种形式的多重继承,通过称为“接口”的特殊类,甚至可以有default
方法。
真正使这两者不同的是通用基类Object
。interface
是你创建一个不继承自Object
的类的方式。
英文:
- No.
In Java, every class is a subclass of Object
, so you can't make an abstract class with only abstract methods. It will always have the method implementations inherited from Object
: hashCode()
, equals()
, toString()
, etc.
- Yes, pretty much.
In C++, for example, there is no specific interface
keyword, and an interface is just a class with no implementations. There is no universal base class in C++, so you can really make a class with no implementations.
Multiple inheritance is not really the deciding feature. Java has multiple inheritance of a sort, with special classes called "interfaces" that can even have default
methods.
It's really the universal base class Object
that makes the difference. interface
is the way you make a class that doesn't inherit from Object
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论