Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Feb 26, 2013, 05:53 PM   #1
moonman239
macrumors 65816
 
Join Date: Mar 2009
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?
__________________
The funny thing about this signature is that by the time you get to the end of it, you will have spent between five and ten seconds doing so.
moonman239 is offline   0 Reply With Quote
Old Feb 26, 2013, 06:22 PM   #2
ArtOfWarfare
macrumors 603
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
Quote:
Originally Posted by moonman239 View Post
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:

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
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:

Code:
[super methodname];
__________________
Don't tell me Macs don't last: 2007 iMac, 2007 Mac Mini, 2008 MacBook Air, all Vintage.
(iMac obsoletion: April 28, 2015, MBA: October 14, 2015, Mac Mini: March 9, 2016)
ArtOfWarfare is offline   1 Reply With Quote
Old Feb 26, 2013, 06:22 PM   #3
naples98
macrumors member
 
Join Date: Sep 2008
Location: Houston
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.
naples98 is offline   1 Reply With Quote
Old Feb 26, 2013, 08:08 PM   #4
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: 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.

-Lee
lee1210 is offline   1 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Tags
objective-c, subclassing

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Subclassing a subclass - question regarding IBOutlets and IBActions moonman239 iPhone/iPad Programming 1 Sep 27, 2013 08:15 PM
Best way to learn Objective C AppleDeviceUser iPhone/iPad Programming 23 Mar 6, 2013 11:57 PM
Printing With Objective C Jitin Jindal Mac Programming 12 Feb 28, 2013 12:01 PM
Subclassing UIDocument nashyo iPhone/iPad Programming 5 Jul 31, 2012 03:37 AM
subclassing UINavigationController Tex-Twil iPhone/iPad Programming 4 Jul 21, 2012 12:59 PM

Forum Jump

All times are GMT -5. The time now is 01:31 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps