PDA

View Full Version : Objective-C: What is subclassing?




moonman239
Feb 26, 2013, 05:53 PM
Please provide an easy-to-understand explanation. Does "subclassing" mean creating a class that is a subclass of another class?



ArtOfWarfare
Feb 26, 2013, 06:22 PM
Please provide an easy-to-understand explanation. Does "subclassing" mean creating a class that is a subclass of another class?

Yes.

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 subclassname : classname {
// instance variables that subclassname has but classname lacks go here
}
// methods that subclassname has and classname may or may not go here. If both have it, subclassname's implementation override's classname's so long as an instance of subclassname is references.
@end

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 methodname];

naples98
Feb 26, 2013, 06:22 PM
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.

lee1210
Feb 26, 2013, 08:08 PM
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.

-Lee