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

Viewed 457

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.

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

Related