Use an interface when you want to force developers working in your system (yourself included) to implement a set number of methods on the classes they'll be building.
Use an abstract class when you want to force developers working in your system (yourself included) to implement a set numbers of methods and you want to provide some base methods that will help them develop their child classes.
Another thing to keep in mind is client classes can only extend one abstract class, whereas they can implement multiple interfaces. So, if you're defining your behavior contracts in abstract classes, that means each child class may only conform to a single contract. Sometimes this a good thing, when you want to force your user-programmers along a particular path. Other times it would be bad. Imagine if PHP's Countable and Iterator interfaces were abstract classes instead of interfaces.
One approach that's common when you're uncertain which way to go (as mentioned by cletus below) is to create an interface, and then have your abstract class implement that interface.
Answer from Alana Storm on Stack OverflowUse an interface when you want to force developers working in your system (yourself included) to implement a set number of methods on the classes they'll be building.
Use an abstract class when you want to force developers working in your system (yourself included) to implement a set numbers of methods and you want to provide some base methods that will help them develop their child classes.
Another thing to keep in mind is client classes can only extend one abstract class, whereas they can implement multiple interfaces. So, if you're defining your behavior contracts in abstract classes, that means each child class may only conform to a single contract. Sometimes this a good thing, when you want to force your user-programmers along a particular path. Other times it would be bad. Imagine if PHP's Countable and Iterator interfaces were abstract classes instead of interfaces.
One approach that's common when you're uncertain which way to go (as mentioned by cletus below) is to create an interface, and then have your abstract class implement that interface.
The differences between an Abstract Class and an Interface:
Abstract Classes
An abstract class can provide some functionality and leave the rest for derived class.
The derived class may or may not override the concrete functions defined in the base class.
A child class extended from an abstract class should logically be related.
Interface
An interface cannot contain any functionality. It only contains declaration of the methods.
The derived class MUST provide code for all the methods defined in the interface.
Completely different and non-related classes can be logically grouped together using an interface.
Videos
I came across this answer for how to use interfaces and traits in unison: http://stackoverflow.com/a/9205347
Could someone explain to me or suggest some reading material as to how abstract classes fit into that answer? Do interfaces and traits used together remove the need for abstract classes?
When is it best to use abstract classes over interfaces and traits? When is it best to use interfaces and traits over abstract classes?
Edit: Thanks to everyone who responded. You have been very welcoming to a new redditor.
I understand a base class provides common methods to other classes that extend it.
And from what I've read, Interfaces provide a way to make sure that a method exists in your class.
It seems that Abstract class combines the two into a single file, so I'm a bit confused on when to use what.
Let's say I have a method called get_device() that is shared amount LaptopDevice, DesktopDevice, and MobileDevice classes.
Each device class gets a list of devices from its device type.
Would I do something like:
abstract class AbstractDevice {
public function get_device() {
return $this->device;
}
}
class MobileDevice extends AbstractDevice {
}or this
class Device implements DeviceInterface {
public function get_device() {
return $this->device;
}
}
class MobileDevice extends Device implements DeviceInterface {
}