Page 1 of 1
Completly Abstract Class vs An Interface
Posted: Sat Jul 05, 2008 5:30 pm
by SidewinderX
Obviously you can define functionality in an abstract class, which can be inherited by a subclass - while an interface does not allow functionality to be defined. Is there a difference/advantage to use a completely abstract class with all abstract functions as opposed to an interface?
Code: Select all
abstract class foo {
abstract public function getName();
abstract public function setName();
abstract public function getAge();
abstract public function setAge();
}
Code: Select all
interface class foo {
public function getName();
public function setName();
public function getAge();
public function setAge();
}
Re: Completly Abstract Class vs An Interface
Posted: Sat Jul 05, 2008 6:15 pm
by Eran
There are two main differences:
1. As you've mentioned, an abstract class can implement some functionality in its methods.
2. An abstract class is inherited while an interface is implemented. Since there is no multiple-inheritance in PHP, you can only extend one abstract class at a time. Conversely, you can implement multiple interfaces. As they say, you only get one chance at inheritance, better make it count.
Re: Completly Abstract Class vs An Interface
Posted: Sat Jul 05, 2008 7:38 pm
by Christopher
SidewinderX wrote:Is there a difference/advantage to use a completely abstract class with all abstract functions as opposed to an interface?
When you create an abstract class you are saying one thing, when you declare an interface you are saying a completely different thing.
With an abstract class you are saying: Here is a not fully implemented class. It is not useful as is. You need to extend it and add some code to complete it. The abstract class may include some finished methods, some methods you need to implement and some support methods that you can use.
An interface says: Here are the method names and parameter lists for a class. If you implement a class with any code you want. If it follows that interface, and you use the 'implements' keyword, then the parser will do check to verify that your class matches the interface exactly. If not you will get an error.
Since they are so different, the question is: Which one do you want to do?
Re: Completly Abstract Class vs An Interface
Posted: Sun Jul 06, 2008 5:20 am
by jayshields
An example would be that a Car could implement Recyclable, and a Poodle could extend Dog.
Re: Completly Abstract Class vs An Interface
Posted: Sun Jul 06, 2008 7:07 am
by Maugrim_The_Reaper
Both abstracts and interfaces do one common thing - which is why they are not completely different. They force you to implement all methods they define as being abstract and compulsory. The difference in content - an abstract contains functional code and is an extendable class. This makes it great for parent classes where all children must share a common interface for composition. An interface is not even a class, it's a list of necessary methods which multiple classes (even unrelated ones with no ancestry) can choose to implement.
Using one instead of the other is not the choice - you can use both. You could have a family of classes with one common parent. The parent may define methods which only subclasses vary depending on the strategy those subclasses represent - so they are declared abstract. In turn, the subclasses could also implement an Interface which is similar to the Abstract enforcement, but because the Interface is common to other interchangeable class families (and makes for a nice common denominator type hint

).
Both however say something similar once you ignore the implementation details: "This is the API I implement". It's all about the end behaviour and expectations.
Re: Completly Abstract Class vs An Interface
Posted: Sat Jul 19, 2008 12:39 am
by Chris Corbyn
A class can adopt multiple interfaces, but it can't extend multiple classes. Interfaces are more about behaviour specification than classes, which are about implementation.