inheritance

Discussion in 'iOS Programming' started by grandM, Mar 4, 2017.

  1. grandM macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #1
    Hi guys

    If you'd have a Bicycle and a Tricylce class it would make sense to let them inherit from a Cycle class. I'm tempted to make the numberOfWheels a property of the Cycle class. Or would you say it's so defining for a Bicycle to have two wheels you'd make it a property of the Bicycle and the Tricycle class and not of the Cycle class?

    I began out with declaring it a property of Cycle. Unfortunately overriding it wasn't as obvious as I thought. I can of course make it happen in the super.init. Is this the best way to go?

    Tx
     
  2. KALLT macrumors 601

    Joined:
    Sep 23, 2008
    #2
    That depends on what the purpose of the Cycle class is. Are you going to instantiate it somewhere or is it purely an abstract class that is only going to be subclassed? Are the Bicycle and Tricycle classes related enough that they need a common parent in the first place?

    An alternative would be a protocol. You could define the numberOfWheels property as a requirement and then make both classes conform to it.
    Code:
    protocol Cycle {
        var numberOfWheels: Int { get }
    }
     

Share This Page