Simple question - how to create a superclass?

So I've recently started programming, and I'm using a book to learn. It's the 2nd edition of Objective-C for Absolute Beginners.


Anyway, I'm up to the 5th chapter which deals with classes, and one of the tasks at the end of the chapter is to create a superclass.


The example is of a bookstore. Let's say I make a class called PrintedMaterials (this will be the superclass). Now I want to make some (sub)classes called Books, Newpapers and Magazines, the superclass of which will be PrintedMaterials.


How do I go about doing this? I can make Objective-C classes under NSObject, but I don't know how to make them subclasses of another class.


I have Xcode 4.2 and I'm running Mac OS X 10.7.2.


Thanks.

Posted on Jan 25, 2012 4:20 PM

Reply
7 replies

Jan 25, 2012 4:52 PM in response to KBar93

// PrintedMaterial.h

// Created by Michael Superczynski on 1/25/12.

// Copyright 2012 HyperNova Software. All rights reserved.


@interface PrintedMaterial : NSObject

{

BOOL available;

}


@propertyBOOL available;



@end



// PrintedMaterial.m

// Created by Michael Superczynski on 1/25/12.

// Copyright 2012 HyperNova Software. All rights reserved.


#import "PrintedMaterial.h"


@implementation PrintedMaterial



@synthesize available;


-(id)init

{

self = [super init];

if (self)

{

available = YES;

}

returnself;

}


// Book.h

// Created by Michael Superczynski on 1/25/12.

// Copyright 2012 HyperNova Software. All rights reserved.


@interface Book : PrintedMaterial

{

NSString *title;



@property(nonatomic, strong) NSString *title;



@end



// Book.m

// Created by Michael Superczynski on 1/25/12.

// Copyright 2012 HyperNova Software. All rights reserved.


#import "Book.h"

#import "PrintedMaterial.h"


@implementation Book



@synthesize title;


-(id)init

{

self = [super init];

if (self)

{

title = @"Learning Objective-C";

}

returnself;

}



-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

PrintedMaterial *thePrintedMaterial = [[PrintedMaterialalloc] init];

Book *book = [[Book alloc]init];

self.book.title = "Learning Xcode";

}

Jan 25, 2012 5:00 PM in response to msuper69

Wow, thankyou so much! But...You have GOT to be kidding me...


Right now, about 50% of that makes no sense whatsoever to me, unfortunately, but don't worry, I'll read up about it all and figure it out! Thanks again.


By the way...I just came across this:


Let's say you make a Books class, and you go to the Books.h file and highlight Books (in front of where it says @interface).


Then go to Edit > Refactor > Create Superclass


How do I use this? It lets me create the PrintedMaterials superclass, but then how do I make Newspapers and Magazined subclasses of PrintedMaterials as well? Do I just copy and paste bits out of the newly modified Books.h file into the Newspapers.h and Magazines.h files?


If so, then that's good, but it looks nothing like above what's been posted above. :O

Jan 25, 2012 5:13 PM in response to KBar93

Here's the minimum you need to know to get started:


NSObject is the most basic type of object in Objective-C. All objects are derived from NSObject.


Hence, PrintedMaterial is a subclass of the superclass NSObject.

Book is a subclass of PrintedMaterial.


You create other subclasses of Printed Material the same as the Book example.


Copy and past Book.h and Book.m and rename every occurrence of Book to Newspaper or Magazine or "whatever".


You'll notice that I did not use the plural form of your object names.


This is because when the object is created (instantiated), it's a single instance of a PrintedMaterial or a Book, etc.


If you want to refer to all Books, then you could put them all in an array and enumerate over the array, get the number of objects in the array, etc.

Jan 30, 2012 5:49 PM in response to KBar93

Hi KBar~


I too am starting out with the same book and saw your post on the other website. I was wondering if you could help me out? I'm stumped on chapter 4. The if else thats halfway down in the Alice exercise. I can't seem to get the IF option "userGuess" in the drop down. I only get continueGuessing and
"keepGuessing". I have tried every combination I can think of and can't seem to make it work. Any assistance you can give would be really appreciatrd. I'm trying to get caught up before this Webnesday nights webmeeting.


Thanks

AndieF

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Simple question - how to create a superclass?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.