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?
3
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.
2
In most OOP languages, an implementing class can derive only from one abstract class, but implement multiple interfaces.
4
In a language like C++ that allows multiple inheritance, and has no interfaces, abstract classes in which all methods are abstract can serve as interfaces. I haven’t worked with C++ that much, but I guess multiple inheritance can cause problems when there are methods with the same name in base classes.
In languages like PHP and C#, interfaces provide a means of achieving similar polymorphism, although I dislike calling it “inheritance” since there’s a conceptual difference between inheriting an abstract class and implementing an interface. Interfaces remove the conflict problem, because they themselves provide no implementation.
An interface serves as a contract to the outside world, while an abstract class can provide an implementation, although if used to “fake” an interface, it most likely won’t.
The main conceptual difference is that when a class inherits another class (abstract or not), there’s a relationship of “is”, so a Car
is a Vehicle
and Dog
is an Animal
. With an interface, it’s what the object does that matters. So both Car
and Dog
can Move()
and the consumer knows it because they implement Movable
, but a Car is definitely not a Dog
, or an Animal
. And the move implementation will be different (wheels vs legs) but the consuming code doesn’t and shouldn’t care. Interfaces are all about the consuming code, rather than the implementation.
The main point is if you have interfaces in you language of choice, use them for things that they’re there for. If not (like in C++), you can fake them by using pure abstract classes.
2
Abstract classes may feature abstract protected methods (in the languages I’m working with), in interfaces methods are usually always public. Whether this difference allows useful exploitations I don’t know.
Edit I first thought that a private abstract method is of no use, but now I recalled that it may be used to ensure that that method is never called. This way you can prevent that a copy constructor of an object is called.
3
Yes, they are different. Otherwise the language designers wouldn’t have provided both. I know of two languages that separate classes and interfaces: Java and C#, mutant clone of Java. The designers created interfaces to avoid supporting multiple class inheritance. Most other languages support multiple class inheritance and consequently don’t separate classes and interfaces.
I think the main difference is that : In an abstract class – even if all the methods are all abstract they can still provide data members (instance variables) and some code to the classes that implement it (in form of private methods or constructors), static blocks; doing part of the work for the sub classes and helping them with the implementation.
One positive side effect : code is in one place so one place to make corrections. sub classes can just call super class method and then do something else or nothing if super class actions are enough for their case. Super class wants each sub class to make this decision so has marked all its implementation as abstract (some could be empty too)
Not relevant to question: but having interfaces means one class can implement two different contracts. So that is an advantage of a interface over a abstract super class
4