Interface Vs Abstract Classes

I would like to keep it simple as much as I can so you it can be easily understood.

An interface is a contract.  An interface is an empty shell. It only contains the variables and methods but no implementation.  There are only the signatures of the methods, which implies that the methods do not have a body. The interface can’t do anything. It’s just a pattern.

Abstract Classes
Abstract classes, unlike interfaces, are classes. They are more expensive to use, because there is a look-up to do when you inherit from those. It contains incomplete and partial implementation of the methods but not complete.

While abstract classes and interfaces are supposed to be different concepts, the implementations make that statement sometimes opposite. Sometimes, they are not even what we think they are.

In Java & .Net this rule is strongly enforced,
In PHP, interfaces are abstract classes with no method declared.
In Python, abstract classes are more a programming trick you can get from the XYZ module and is actually using metaclasses, and therefore classes. And interfaces are more related to duck typing in this language and it’s a mix between conventions and special methods that call descriptors (the __method__ methods).

So the best thing its always practice and practice.

Leave a Reply

Your email address will not be published. Required fields are marked *