|
Posted by Willem Bogaerts on 05/25/07 06:57
> An API function gives me an object of class 'Blah'. I have subclassed
> 'Blah' and added a few methods:
>
> class EnhancedBlah extends Blah {
> function enhancement1() {}
> function enhancement2() {}
> }
>
> Is there a way to 'downcast' the object to a subclass? Something like
> this:
>
> $blah = SuperApi::getBlah();
> $enhancedBlah = (EnhancedBlah) $blah;
No. That would break the substitution principle. It is the other way
around. You can create an instance of the subclass and treat it like you
would treat an instance of the superclass. Subclasses are more detailed
than superclasses, so you would need extra info when downcasting. In
fact, superclasses are usually generic terms, and therefore abstract
(non-technical meaning here), even if they don't have abstract methods
in the technical sense.
An everyday example:
The superclass "policeman" has subclasses "inspector", "traffic
policeman", etc. The common functionality gets in the superclass. "make
arrest", for example. You can ask any member of a specific subclass to
perform his duty as a member of the superclass. But not the other way
around. In fact, there is no such thing as a "policeman" instance: every
policeman is a specific member of a subclass of "policeman".
Best regards.
--
Willem Bogaerts
Application smith
Kratz B.V.
http://www.kratz.nl/
[Back to original message]
|