Interfaces vs Abstract Classes with Only Abstract Methods – Key Differences

abstract classinterfacesobject-orientedprogramming-languages

Let's say we have an abstract class and let this class has only abstract methods. Is this abstract class different from an interface that has same methods only?

What I am looking to know is if there are any differences both philosophically, objectively and in the underlying programming language implementation between an Abstract Class with only abstract members and an equivalent Interface?

Best Answer

Technically, the differences aren't really significant but, conceptually, they are entirely different things and that leads to the technical differences others have mentioned.

An abstract superclass is exactly what it sounds like, it's a common type that is shared by many other types, like Cats and Dogs are Animals.

An interface is also exactly what it sounds like, it's an interface through which other classes can communicate with the object. If you want to make a Cat Walk, you're ok, cause Cat implements a CanWalk interface. Same for a Lizard, though they walk very differently. A Snake, on the other hand, does not implement CanWalk, so you can't tell it to Walk. Meanwhile, Lizard and Snake (or possibly more explicit subclasses -- I'm not an expert) might both shed their skin, and thus implement CanShed, while a Cat couldn't do that.

But they're all still Animals and have some common properties, like whether they're alive or dead.

This is why all methods on an interface must be implemented as public (or explicitly, in C#). Cause what's the point in an interface that's hidden from the class interfacing with the object? It's also why you can have multiple interfaces to an object, even when a language doesn't support multiple inheritance.

To go back to your question, when you look at it this way, there's very rarely a reason to have an entirely abstract superclass.