Understanding Subclassing

subcategoriesHere is the definition of subclassing,

In object-oriented programming, inheritance is a way to reuse code of existing objects. In classical inheritance where objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes called base classes, superclasses or parent classes. The resulting classes are known as derived classes, subclasses or child classes. The relationships of classes through inheritance gives rise to a hierarchy.

From Wikipedia (2013).

This means that if you have code in one class that you would like to reuse in another class you can create a subclass. The original class is called the superclass and the subclass gets all the code in the superclass. You can also add additional code to the subclass. Programmers do this all the time which creates a hierarchy like the graphic at the top of this page.

The Super And Self Keywords

If you haven’t yet groked the relationship between classes and objects the details of this can get confusing. For instance, take a look at the init override that you get from Xcode’s code library:

-(id)init{
    self = [super init];
    if (self) {
        //initialize here
    }
    return self;
}

NOTE I’m assuming you know about how to create classes in Objective-C here. If you don’t know what I’m talking about click here to read the chapter on creating classes in Objective-C.


So I highlighted some particularly confusing keywords that are in the init function. self and super reference objects from the class that we are currently defining.

While both keywords objects created from the class self references the properties and methods defined in the current class definition while super references the properties and methods defined in the superclass definition.

Objects And Classes

Let’s take another look at that init override so we can see the context where we usually see this function.

#import <Foundation/Foundation.h>

@interface MyClass : NSObject

@property(strong) NSString *name;

@end

@implementation MyClass

-(id)init{
    self = [super init];
    if (self) {
        //initialize here
        _name = @"MyClass Object";
    }
    return self;
}

@end

int main(int argc, const char * argv[]){
    @autoreleasepool {
        MyClass *myObject = [[MyClass alloc] init];
        NSLog(@"myObject.name = %@", myObject.name);
    }
    return 0;
}

Here’s the English version of the code above: we defined a class named MyClass which is a subclass of the NSObject class. MyClass is going to override the init function.

In the main function we are going to instantiate an object named myObject and write out the myObject name value to the console log.

Class Vs Object

A class is a definition of an entity (an abstract model) that is made up of definitions for properties and methods. By itself, a class doesn’t do much other than contain a definition. In the above example all the code that defines the class is between the @interface and @end keywords and the @implementation and @end keywords.

Objects are special variables that are created based on class code definitions. You can see how objects are used in the main function above. Typically, you would create many objects based on one class definition.

How It Gets Confusing

This stuff gets confusing because most people think of classes and objects as the same thing. After all, when you want an object to behave differently you must make changes so rewrite the class definition. So that’s pretty natural. Getting back to the init function though you can see why this gets confusing,

-(id)init{
    self = [super init];
    if (self) {
        //initialize here
    }
    return self;
}

In that first line of code after the function name, self = [super init];, it looks like we are getting an NSObject object from the superclass, assigning this object to self and then returning this to the caller.

This is not really the case. You are simply getting your MyClass object from the superclass. The reason that it seems like you are getting an NSObject object back here is because you are thinking in terms of objects and not class definitions. Even though this code will be used to initialize and return objects at some point right now we are only working on the code definition.

That self keyword doesn’t represent some NSObject object variable out there. It’s simply a way to reference the code from the superclass definition.

What subclassing really does for you is more like a copy and paste. It’s as if you have copied all of the NSObject code and used this code in MyClass. So that init call to super will just initialize the part of the class definition that is contained in NSObject before you finish up initializing.

iOS Code Camp Just Got A Crazy Bonus!

app-globe

iOS Code Camp my online boot camp style training program. What I did in the past was take a small group of serious technologists and taught them everything they need to be iOS developers. This all takes place on my website over a period of3 now 6 … [Continue reading]

What Are The Two Biggest Complaints That Frustrate Newbie iOS Developers?

frustrated-women

Does this sound familiar? "I would love to do iPhone development but I just don't have the time to wade through the obtuse and elitist tools required to master iOS programming." I'll bet this describes at least some of you out there right? The reason … [Continue reading]

Create a New Mac Cocoa App

objective-c-tips

Usually, our blog readers are more interested in making iPhone apps (hence the title of the blog). But, with the Mac app store and the demands of many users who want desktop versions of apps many developers also want to implement a Mac app. Here's … [Continue reading]

Can You Help Me Out?

Hey could you do me a solid and fill out this brief survey?  https://www.surveymonkey.com/s/5DR2FDP < it'll influence what I work on this year!   … [Continue reading]

Working with Multiple Targets in Xcode + Code Reuse, Part One

objective-c-tips

Have you ever wanted to share code between apps? I have and honestly, code reuse with Xcode (in particular between apps) is not as straightforward as you might imagine. You do have some options which I'll talk about here and once you get something … [Continue reading]

APE: Author, Publisher, Entrepreneur Book Preview

ape-1667x2500

APE is an new eBook by Guy Kawasaki and Shawn Welch about how to publish books in the digital landscape. For those of you who don't, Guy Kawasaki has written many books about tech and entrepreneurship that are always well done and well received. … [Continue reading]

Is It Over for Indy Mobile App Developers?

hungry-bird

Here is why I ask: for years, I've done pretty well on the app store with Tasting Notes and some other apps. In fact, Tasting Notes still sells copies every single day. Sales are not gangbusters but they are consistent. However, while Tasting Notes … [Continue reading]

Write to the Console with NSLog

objective-c-tips

Writing to the console log is something that you'll end up doing sooner rather than later. Usually you'll write to the log when your testing prototype code. Other times, you may be simply debugging your code (although there are better ways to … [Continue reading]

Using Xcode to Make a New iOS App

objective-c-tips

Setting up an iOS application with Xcode is very straightforward. The process is a lot like creating a new text document or Keynote presentation. Here's how to do it. Install Xcode Xcode is the IDE, Integrated Development Environment, used to create … [Continue reading]