Completly Abstract Class vs An Interface

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Completly Abstract Class vs An Interface

Post 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();
}
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Completly Abstract Class vs An Interface

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Completly Abstract Class vs An Interface

Post 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?
(#10850)
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: Completly Abstract Class vs An Interface

Post by jayshields »

An example would be that a Car could implement Recyclable, and a Poodle could extend Dog.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Re: Completly Abstract Class vs An Interface

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Completly Abstract Class vs An Interface

Post 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.
Post Reply