2

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.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Cheolho Jeon
  • 359
  • 1
  • 12
  • I think the following link may be helpful: [interface-vs-abstract-class-general-oo](https://stackoverflow.com/questions/761194/interface-vs-abstract-class-general-oo) – Gorisanson Jul 23 '20 at 08:46
  • 2
    Does this answer your question? [Difference between abstract class with all method abstract and interface?](https://stackoverflow.com/questions/23302920/difference-between-abstract-class-with-all-method-abstract-and-interface) – Hülya Jul 23 '20 at 08:57
  • @Hülya Yes, kind of, but my question is not about how abstract classes and interfaces are **used/perceived** but rather **implemented**. I guess writing an abstract class with all abstract methods is a bad design choice. But apart from it being bad design choice, are they essentially equivalent? On a second thought, my question can be paraphrased as "Can we separate type from class?" or "does class imply a type?" – Cheolho Jeon Jul 23 '20 at 09:33

1 Answers1

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

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

Matt Timmermans
  • 53,709
  • 3
  • 46
  • 87