Objective-C: What is subclassing?

Discussion in 'Mac Programming' started by moonman239, Feb 26, 2013.

  1. moonman239 macrumors 68000

    Mar 27, 2009
    Please provide an easy-to-understand explanation. Does "subclassing" mean creating a class that is a subclass of another class?
  2. ArtOfWarfare macrumors G3


    Nov 26, 2007

    A subclass inherits methods and instance variables from its superclass.

    If you have a class called classname and you'd like it to have a subclass called subclassname you'd declare it like so:

    @interface [b]subclassname[/b] : [b]classname[/b] {
    // instance variables that [b]subclassname[/b] has but [b]classname[/b] lacks go here
    // methods that [b]subclassname[/b] has and [b]classname[/b] may or may not go here. If both have it, [b]subclassname[/b]'s implementation override's [b]classname[/b]'s so long as an instance of [b]subclassname[/b] is references.
    A subclass can override a method it inherits from the class it is based on. The class it is based on is called its superclass. If subclassname overrides a method named methodname it inherits from classname but it'd like to use the classname variation on methodname instead of its own, it can do something like:

    [super [b]methodname[/b]];
  3. naples98 macrumors member


    Sep 9, 2008
    Subclassing is just a subclass of another class and it inherits behavior and members of its parent class.

    For example, you may have a "car" class which has properties that are common to all cars such color, tires, engine, etc. It also has behaviors that are common to all cars such as drive forward, drive backwards, stop, etc.

    Now for some reason you need to differentiate between different types of cars but you don't want to repeat all those properties and behaviors in each new class so you write subclasses of the "car" class.

    So you may have subclasses such as "manual cars", "automatic cars", "convertibles", "electric cars", etc. All of these subclasses have some common traits inherited from the "car" class but will also have some unique characteristics that can be included in the subclass.
  4. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    You can think of the inheritance as a "is a" relationship. Another approach, composition (one class with a member that is another class) is a "has a" relationship. When you're building a new class that is related to others, you decide what kind of relationship it should have.
    Note that Objective-C does not support multiple inheritance, which means a class that *is* a number of other classes. With composition the skies the limit.

    A pet store is a store.
    A pet store has cages.
    A cage has an animal in it.
    A dog is an animal.
    A dog has a collar.
    A collar has a dog tag.
    A dog tag is a tag.

    A burrito is a food product.
    A burrito has beans.
    A bean is a food product.
    A burrito has chicken.
    Chicken is a food product AND is an animal.

    The last one is a stretch, and since there's no multiple inheritance you'd probably have a living chicken that is an animal and a not living chicken that is a food product. You could have a slaughter method on a chicken that yields the food product.


Share This Page