Advertisements

Objects (Part 4): Inheritance—Some Loose Ends


Overriding

As mentioned before, it is very possible (and often encouraged) to override inherited methods to provide a more meaningful implementation. The exact method to invoke (the original or the overriding version) is determined by the class of the receiver of the message at runtime. It is still possible to invoke the original method by sending a call to super. This can be demonstrated by overriding the dealloc method, inherited from NSObject. To demonstrate, add the following code:

// Fraction.m
- (void)dealloc {
	numerator = 0;
	denominator = 0;
	[super dealloc];
}

This method does very little, except to call NSObject’s dealloc method when Fraction’s is called (We are also setting the ivars to zero). dealloc is a method related to memory management; basically it involves releasing memory held by objects. Because Fraction does not contain any objects as ivars, we don’t actually have anything to release.

We can use the same skeletal implementation in MixedNumber, where we simply call Fraction,’s dealloc, which in turn calls NSObject’s.

// MixedNumber.m
- (void)dealloc {
	[super dealloc];
}

Because MixedNumber inherits from Fraction, whatever gets released by Fraction is also released by by MixedNumber. As a matter of fact, if you forget to include [super dealloc]; in your dealloc method, you will get a compiler warning.

A later lesson will focus on categories, which allow you to add methods to a class, including classes you don’t have access to the code for, such as Apple’s own Foundation and UIKit classes.


Note that it is not possible to override ivars; however, as MixedNumber shows, it quite easy to add a new ivar. In addition, any ivars (well, almost any ivar; there are certain privacy controls, although they are rarely used) that a superclass declares are directly accessible in the subclass.

There is one pitfall involving inherited ivars, and it has to do with pointers. If you do a direct assign, such as subclassVar = superclassVar;, the former is simply a pointer (assuming that both are objects) to the superclass’s ivar, which means that if it gets freed or changed in the superclass, then the same will happen in the subclass. The solution (assuming that the above is undesired, as it usually is) is to declare a @property with retain as one of the parameters, as in

@property (nonatomic, retain) NSString *myString;

in the header file of the class. Note that retain can only be applied to a property for an object, but remember that primitive types are not passed as pointers anyway, so the above issue does not exist.

Abstract Classes

There exist (and you can create) classes that are not designed to be used on their own, but merely as an object to be subclassed. For example, you will almost never create an NSObject—you’ll use a subclass. NSObject, therefore, is an abstract class.

A variant is a class cluster, such as Foudation’s NSNumber, which is what you’d create—but in fact, behind the scenes there are a number of subclasses of NSNumber that hold different types of numbers. You don’t interact with these subclasses, but only with NSNumber. These classes, along with NSNumber, for the class cluster.

Advertisements
Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Advertisements
  • Welcome

    My goal is to make CupsOfCocoa into a beautiful source for beginners to the iPhone platform to get started. Subscribe below for more, and stay tuned!

  • Contact Me

    If you need to contact me for any reason, feel free to send me an email.
  • The Giving Spirit

    If you've found this site helpful, would you consider donating a little sum? Any amount is appreciated...Thanks so much!

  • Roadmap

  • Enter your email address to follow this blog and receive notifications of new posts by email.

    Join 222 other followers

  • Back to the Past

    February 2011
    S M T W T F S
    « Jan   Mar »
     12345
    6789101112
    13141516171819
    20212223242526
    2728  
  • Time Machine

  • You count!

    • 621,662 views
  • Worldwide Stats

    free counters
  • Advertisements
%d bloggers like this: