Abstract Classes

In Chapter 10 we saw that if a derived class does not implement all the methods of an interface, then it must be declared abstract. Let's push this concept to the extreme and see what a completely abstract class might look like. Listing 11-1 shows the definition of such a class.

Listing 11-1. The definition of the abstract class Bird
❶abstract class Bird{
  protected $plumage;
  protected $migratory;
  abstract public function __construct();
  abstract public function fly();
  abstract public function sing();
  abstract public function eat();
  abstract public function setPlumage($plumage);
  abstract public function getPlumage();
  abstract public function setMigratory($migratory);
  abstract public function getMigratory();
}

As we saw in Chapter 10, any class that contains abstract methods must include ❶ the keyword abstract in its class declaration. That class may have any number of data members and any number of methods with or without an implementation. However, if a method lacks an implementation it must also be declared abstract.

The class in Listing 11-1 has data members declared as protected, making them available to derived classes. This class could be termed a pure abstract class because all of its methods are abstract. Note that all the methods of this class are declared public. Let's see why that is so.

Private Methods Can't Be Abstract

Methods identified as abstract cannot be private; they must be either public or protected. The reason is that an abstract private method is a contradiction in terms. Because an abstract class has undefined methods it cannot be instantiated (it only exists to be the parent of a derived class). A class with abstract private methods could never be implemented because private methods cannot be inherited. The same reasoning would apply to a final abstract method.


Note:

Recall that a final method cannot be changed in a derived class. An abstract method cannot be final because it must be overridden—i.e., changed.


How does a pure abstract class, with no defined methods, differ from an interface? An interface may not have data members or a constructor. (This may change in future versions of PHP. There is some discussion of allowing interfaces to have constructors.) In order to turn the Bird class, shown in Listing 11-1, into an interface you would have to replace the keywords abstract class with interface and remove $plumage, $migratory, and the constructor. Although interface methods are effectively abstract, you still need to remove the abstract descriptor for each method.

Interface or Pure Abstract Class?

You now know the syntactic differences between interfaces and pure abstract classes, but when should you use one rather than the other? In general, it's probably better to use an interface than a pure abstract class because of the flexibility of interfaces. PHP doesn't allow multiple inheritance for classes; a child class may have only one parent class. However, you can implement any number of interfaces.

It makes more sense to use abstract classes when there is a mix of concrete and abstract methods. You can provide an implementation where identical, derived class behavior is expected, and you can provide an abstract method where behavior will differ. You could, of course, ignore methods for which you expect the behavior of derived classes to diverge, but by declaring a method abstract you ensure that it will be implemented in any derived class. You'll see how this can be used to your advantage in the following discussion of polymorphism.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.160.216