I am trying to learn OOP in PHP, and I have some confusion about interfaces and abstract classes. They both contain no implementations, only definitions, and should be implemented through their sub-classes. What part of abstract classes clearly distinguishes them from interfaces? Also, due to their apparent similarities, based on what reasons should I decide to use one over the other?
Explanation: The abstract classes can be used to create a generic, extensible class library that can be used by other programmers.
Java Abstract class is used to provide common method implementation to all the subclasses or to provide default implementation. We can run abstract class in java like any other class if it has main() method.
It is possible to create abstract classes that contain concrete members, such as methods or properties. You still can't instantiate the abstract class directly, but any instantiated sub-classes can benefit from implemented members defined in the abstract class.
An interface, by comparison, never contains any implementation logic. It is down to each implementing class to provide the implementation of all members defined in the interface.
In terms of how I view the differences, a sub-class of an abstract is-a class of that type. e.g. Dog
is an Animal
. I see an interface as a does-a relationship. e.g. ICanDisplayImages
tells me that the implementing class can display images, but tells me nothing about what the class actually represents.
An abstract
class forms an is-a relationship between itself and the subclass, while an interface
creates a follows-a relationship. As such, abstract classes are much more concrete than interfaces and they may also contain concrete implementations (e.g. Template methods) while an interface defines a contractual set of methods an implementing class must follow. This is a much higher level of abstraction as the implementing class must not necessarily be of the abstract class. Use it to standardize your API.
Related questions: https://stackoverflow.com/search?q=abstract+vs+interface
Beyond the OOP philosophy described in other answers, I think the major use of abstract class is kind of a skeleton.
It's usefull when you design an application, or work with a team, because you have a base code to work with and extends it's close to interface but with a more advanced workflow.
They both contain no implementations..
Abstract class can implements all or only part of the methods. But the main philosophy - is extend existing abstract class by to add a new methods in child classes (that extend a basic functionality).
In a child class you can extend only one class (abstract class). Abstract class define functionality that you must implement or just extends by other methods in child classes.
Interfaces are use to define the class behaviors. You can implement more than one interface(and say my child-class must do this,this and this thigs!), but you can extend only one abstract class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With