Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Chapter 7. Working with Object-Oriented ... > Overloading Base-Class Methods

Overloading Base-Class Methods

You can also overload methods. When you overload a method, you give it multiple definitions, and Objective-C chooses the correct version of the method based on the parameter list—the type and number of parameters must be different for each version of an overloaded method.

Listing 7.9. Starting overload.m.

#import <stdio.h>
#include <Foundation/Foundation.h>

@interface Class1: NSObject
-(void) print;
@end

@implementation Class1
-(void) print
{
    printf("Hello there.\n");
}
@end

@interface Class2: Class1
-(void) print: (int) x;
@end

@implementation Class2
-(void) print: (int) x
{
  printf("Your number is %i.\n", x);
}
@end


int main(void)
{
  Class2 *c2 = [Class2 new];

  [c2 print];
  [c2 print: 5];

  return 0;
}
       .
       .
       .

					  


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial