Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
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 6502a
 
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 601
 
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];
__________________
Battery Status - On the Mac App Store
The only app that'll estimate when your wireless devices will need their batteries changed.
Including the ones paired with other Macs on your network.
ArtOfWarfare is offline   1 Reply With Quote
Old Feb 26, 2013, 06:22 PM   #3
naples98
macrumors member
 
Join Date: Sep 2008
Location: San Diego
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 online now   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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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

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

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC