Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Otto Wichmann
4,827 PointsSuperclass and subclass
As I follow the tutorials on Treehouse I also have a couple books open on my desk. One of those books is called "Objective-C for absolute beginners" and it has a "challenge" that I am trying to complete. (In chapter 5)
They want me to create a superClass called "Printed Material" and that superClass will have subClasses call "Book" "Magazine" and "Newspaper". The idea is that I will then import "Printed Material" to "book" and so on and all the properties from "Printed Material" will be inherited by "Book".
I first thought that all I had to do was create a new Objective-C class. (That I know how to do) and then I will get a header file. In that header file I can add all the basic properties that I want all my subClasses to have. But then I get confused. Do I need to create this other subClasses in the implementation file from "Printed Material" Or do I create a new Objective-C class call "book" with Parent NSObject and then #import "Printed Material" to the header of "book".
When I look online for some help I could only find very advance syntax that used methods like "BOOL" (I think thats a method at least) and a whole lot of "self." and "init" and so on and so forth. (here is the link I found https://discussions.apple.com/thread/3685159?start=0&tstart=0) But I don't really understand what that person is doing.
Am I reaching to far ahead? Should I just keep moving with the other chapters and come back to this one later? Or is there a simple way to create this classes and superClasses?
Also if anyone else out there red this book, I was wondering what type of methods are we supposed to implement for the "Sale" object. The author only helps with the "customer" method and thats an NSArray. Are all the method for the "sale" object also NSArrays?? Wouldn't that be redundant??
Just in case anybody wants to help with that one here is what the object looks like
/*
"SALE"
(This are all NSString*)
Customer;
Book;
Date;
Time;
Amount;
Payment Type;
(This are all the Methods we need to implement)
Charge credit card;
Print Invoice;
Checkout ;
*/
Well I should get back to reading the books I have at hand. Thank you in advance for any help or guidance.
Cheers,
Otto.
1 Answer
Amit Bijlani
Treehouse Guest TeacherSeems like there's a bit confused about the semantics.
The header (.h) defines what the file will do while the implementation file which ends in .m implements them.
Every class in Objective-C is a subclass of
NSObjectbecause it provides basic memory management functionality and access to the runtime. If you are not subclassingNSObjectthen most probably up the chain some parent class that hasNSObjectas its parent.In your case the parent class "Printed Material" would be defined as:
@interface PrintedMaterial : NSObject
@end
The other subclasses would have PrintedMaterial as its parent class.
@class PrintedMaterial;
@interface Book : PrintedMaterial
@end
Notice how the header file simply defines @class PrintedMaterial instead of importing because it's simply instructing the compiler that there's a class defined elsewhere. In your .m file or implementation you will have the actual import statement like: #import "PrintedMaterial.h.
Otto Wichmann
4,827 PointsOtto Wichmann
4,827 PointsOk great, I think its starting to make sense.
Basically I have to create the new objective-C class. In my case "printedMaterial". Then I will give that class some properties (Like Title; Publish date; and so on).
Then I will have to create another Objective-C class. This time it will be book. Now I will have 4 files [Printed material.H and printed Material.M and I will also have book.H and book.M]
Then in book.h I will simply define @class PintedMaterial; THEN the @interface will be book : printed material INSTEAD of @interface book : NSObject. (thats what the printed material.h has already)
Then I can still add SPECIFIC properties to book.h that I can later implement in book.m but because book.h is now a "subClass" of printedMaterial.h all the properties I wrote for printedMaterial ALSO belong to (or where inherited by) book object.
ALSO in book.m I will have to #import "printedMaterial.h"
Is that kinda how it goes??
Thank you Amit!
Otto
Amit Bijlani
Treehouse Guest TeacherAmit Bijlani
Treehouse Guest TeacherYep you got it. Subclassing is also known as inheritance, which means that when you inherit from a parent the child gets access to all the parents properties and methods. Although there are some exceptions but lets not complicate it for now :)
Just one note, keep in mind that a class name is always capitalized so it will be
PrintedMaterialwith a capital P and M. Similarly it will beBookwith a capital B. When create an object is where you lower case the first letter.Otto Wichmann
4,827 PointsOtto Wichmann
4,827 PointsThank you Amit!!