Jump to content
Larry Ullman's Book Forums

Placing Variables In Subclasses And Not In The Abstract Parent


Recommended Posts

Hello,

 

 

On page 301 where it declares the abstract class and on the next page where it extends it, it turns out that no attribute is declared in the parent but only in the child class, namely triangle class.

 

Since the parent is called "shape", wouldnt one put the main attributes there such as "$side" so that other shapes like rectangle can also use that ?. What would we do then when we create another extension like "class rectangle extends shape" , we are going to need the $sides again.

 

 

thank you

 

 

A

Link to comment
Share on other sites

Yes you will need $sides again, but this is one of the underlying principles of more advanced OOP. The parent class is a framework for the sub class to take minimal information from and develop it's own set of methods for that particular shape. The $sides property can never be used by the Shape class and overwriting the Shape property each time you extend the class goes against the ideas of how OOP should be used.

Link to comment
Share on other sites

Well I did think about that but then started to think, in my mind that I couldn't see a problem with declaring a property in the abstract class. Something like

 

private $side;

 

But then didn't put those 2 ideas together into a form of an answer as I confused myself. Thought it was best to wait for a difinitve answer

Link to comment
Share on other sites

Thank you. It is clear to me now

=================================

 

Yes you will need $sides again, but this is one of the underlying principles of more advanced OOP. The parent class is a framework for the sub class to take minimal information from and develop it's own set of methods for that particular shape. The $sides property can never be used by the Shape class and overwriting the Shape property each time you extend the class goes against the ideas of how OOP should be used.

Link to comment
Share on other sites

 Share

×
×
  • Create New...