PHPBuilder - OO Design: Abstract Classes Page 2

RSS Twitter

OO Design: Abstract Classes - Page 2

by: Jonathan Griffin
February 10, 2003

What Is An Abstract Class?

An abstract class is a base class that is not instantiated. It provides common functionality to be inherited by subclasses. Another way to think of an abstract class is like a template. An abstract class contains no code, but instead, defines what code must be defined in subclasses.
Why create a class without any code and one that cannot be instantiated? Well, there are many situations where abstract classes are quite useful. Let's take a look...

Designing With Abstract Classes

One common use for an abstract class is at the top of an object hierarchy. Perhaps you are designing an application in which you would prefer all of your objects to share a certain set of common behavior, such as toString(). Regardless of what each object models within your business domain, you can reliably call upon this expected behavior, as long as implementation has been provided. This is an excellent situation where an abstract class will provide us with a definition for common behavior.
An interface definition is another great example of using abstract classes within your application design. In languages like Java, interfaces are defined with the type interface, and contain public method signatures exclusively. If you try to put implementation into an interface in Java, you will get a compile error. PHP does not have an interface type as such. However, you can still get the same feel, by using an abstract class and keeping out any implementation. With an interface, a certain behavior is defined that any class can implement in any way it sees fit. Furthermore, these interfaces can be composed within classes to extend their functionality even further. Later on, we'll take a look at how these interfaces can be dynamically pluggable at runtime.

« Previous Page
Next Page »

Comment and Contribute

Your comment has been submitted and is pending approval.

Jonathan Griffin



(Maximum characters: 1200). You have characters left.