Re: A Good OOP Tutorial/Read?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 17 May 2013, at 14:04, Tedd Sperling <tedd.sperling@xxxxxxxxx> wrote:

> I thank you for your addition, but what you provided did nothing to explain the difference between abstract and interface.
> 
> In your example:
> 
>    An abstract Shape with Circle and Square inheriting.
> 
> OR
> 
>    An interface Shape with Circle and Square implementing.
> 
> Does exactly the same thing -- so where's the difference?

An interface does what it says on the tin: it describes an interface that a class can then tell the world it implements.

An abstract class provides functionality as well as an interface description. An abstract class cannot be instantiated, it can only be extended.

The logging example given by someone earlier in this thread is the most common example given for interfaces. This is where the interface is defined as an API for others to implement and/or use which then enables users to pick and choose the combination of implementations they want to use based on their requirements without needing to change any of the code of either class.

Abstract classes are more likely to be used where there is a set of shared functionality that is required by several other classes, but which is not functionally useful by itself. An example of this from a current project of mine is in aggregating news sources (RSS, scraping, custom formats, etc).

I have an abstract base class which supplies common code to parse and process data, and some abstract methods that must be implemented by any class that extends it because the abstract class methods may call them. The abstract class also contains a static factory method for getting instances of the type-specific classes.

With this architecture the users of the abstract class never see the derived classes so there's no need to publish an interface for them to use.

Does that help at all?

-Stuart

-- 
Stuart Dallas
3ft9 Ltd
http://3ft9.com/
-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php






[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux