What is the difference between an interface and an abstract class?
Well, it's not the same thing.
Question
What is the difference between an interface and an abstract class?
Less Short Answer
There are two major differences. First, an abstract class has a constructor, which is called to construct your final object — this is not the case for an interface. Second, an abstract class can carry a mutable state, which again is not the case for an interface.
javaabstract class Vehicle {
private int speed; // mutable state
protected Vehicle(int speed) { // constructor
this.speed = speed;
}
}
interface Movable {
// no constructor, no mutable state allowed
}A Word of Caution
This question may sound like a trick question, because you can have static and instance methods in both abstract classes and interfaces. It is a feature that was added to interfaces in Java 8, in 2014.
One Last Word
Now, you may be wondering: when should one use abstract classes or interfaces? By default, prefer interfaces. Why? Well, that will be for another time.