|
|
#1 |
|
Objective-C: What is subclassing?
Please provide an easy-to-understand explanation. Does "subclassing" mean creating a class that is a subclass of another class?
__________________
Now available on the iPad App Store: Clean Cuisine Click here to load its App Store page. |
|
|
|
0
|
|
|
#2 | |
|
Quote:
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: Code:
@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
Code:
[super methodname];
__________________
Battery Status - On the Mac App Store
The only app that'll estimate when your wireless devices will need their batteries changed. Like it on Facebook! |
||
|
|
1
|
|
|
#3 |
|
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. |
|
|
|
1
|
|
|
#4 |
|
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 |
|
|
|
1
|
![]() |
|
| Tags |
| objective-c, subclassing |
«
Previous Thread
|
Next Thread
»
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
All times are GMT -5. The time now is 07:57 AM.








Linear Mode
