jeudi 23 juillet 2020

Is an abstract class without any implementation and variables effectively interface?

I'm reviewing the concepts of OOP, reading .

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:

  1. 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:

  1. 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.

Thank you for spending your time on reading this question! Have a great, safe day.

Aucun commentaire:

Enregistrer un commentaire