This question already has an answer here:
- Interface vs Abstract Class (general OO) 33 answers
I have searched around SO as well as the rest of the web for a good answer but I have't found one that I really understand. I am going to present this in a different way and hopefully the answers will help others as well.
As far as I understand, the two concepts have the same rules except an abstract class is more flexible due to the method implementation ability. Also, I am aware you can implement multiple interfaces and only extend a single class but I'm sure there are more differences than the two I mentioned.
Please look at the two snippets of code and give me an example what I can do with each of my examples that would make me want or not want to use the other.
Abstract Class
abstract class Foo {
abstract public function getValue();
abstract public function setValue($value);
}
class myObj extends Foo {
function getValue() {
}
function setValue($value) {
}
}
Interface
interface Foo {
public function getValue();
public function setValue($value);
}
class myObj implements Foo {
function getValue() {
}
function setValue($value) {
}
}
I have thought about this before, and the best I could conclude was that interfaces are a logically convenient abstraction of a pure abstract class (c++).
As for why you would choose interfaces over abstract classes, I quote (a c++ source but the concepts are the same):
The thing is, when using interfaces, the first thing that comes to mind is decoupling. When using an interface, the user and the implementing-class are totally decoupled. The same applies for when you're using a pure abstract class which is basically an interface.
Briefly speaking, interface is to standardize a set of functions,while abstract class is to define a basic skeleton for classes to derive from.
To resume the idea (globally, not in detail):
is the notion to
extend from something
, and optionally add some new feature or override some existing feature (to do differently). But using inheritance, you share a big part of code with the parent. You are a parent + some other things.is representing some abilities (we says a class is implementing an interface to says that it has these abilities). An interface can be implemented by 2 classes which are completely different and do not share their code (except for methods they implements). When A and B are implementing interface C, A is not a B and B is not a A.
And one of the reason for
interface
is indeed to allow programmer to do the same as they could do with multi-inheritance, but without multi-inheritance problems.This notion is used in some programming languages like JAVA, PHP...
Abstract
Abstract Classes focus on a kind of things similarity.
People are considered of type
mammal
and as such would not be considered of typevehicle
.Interface
Interfaces focus on collation of similar function.
For example: You are a human being and are of type
mammal
. If you want to fly then you will need to implement aflying Interface
. If you want to shoot while flying, then you also need to implement thegun Interface
.See the examples below: