1. A Change of Season
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Mar 2004
    Next Door
    Rep Power

    Abstract class and methods, favor composition over inheritance...

    Someone, please explain these:

    1 - "If every subclass needs to define its own method for determining what it's area should be (for example circle, square etc), then we create an abstract method in the parent class and we refer to it as Template Method Design Pattern". Sub-class must provide the content of the method.

    2 - "If the parent class is designed to extract common behavior, we make it abstract."

    3 - "Favor composition over inheritance".

    4 - Is this a new php thing [$jeff]?
    PHP Code:
    $staff = new staff([$jeff]); 
    Last edited by English Breakfast Tea; February 23rd, 2018 at 01:40 AM.
  2. #2
  3. Backwards Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Washington, USA
    Rep Power
    1. The area of a shape is an important method to have but the parent class can't know how to calculate it. Making the method abstract requires that subclasses implement it.
    2. See #1.
    3. You know how some people will make, like, a user class for user data and make it extend the database so they can do database stuff? That's inheritance. It's also wrong. They should be making the user class get a database object (somehow). That's composition.
    4. [] is shorthand for array().
    PHP Code:
    $staff = new staff(array($jeff)); 

IMN logo majestic logo threadwatch logo seochat tools logo