Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Method overload in Objective-C

Hello, although "The Objective-C Programming Language" says that Objective-C doesn't support to overload methods with the same name and the same type of parameters. I have found it possible. for example these two method overload correctly:

- (void) setX: (int) valueX adding: (int) valueToAdd;
- (void) setX: (int) valueX Y: (int) valueY;

This is due to the name mangling representation in the object file.

iBook, Mac OS X (10.4.6)

Posted on Jun 7, 2006 8:50 AM

Reply
Question marked as Best reply

Posted on Jun 7, 2006 8:27 PM

(I'm a beginner, so forgive me if this is wrong.)

AFAIK, those are two different methods, not because of "name mangling" but simply because they have different names.

setX:adding: is not the same as setX:Y:
6 replies

Jul 17, 2006 7:30 PM in response to tele_player

There isn't really a maybe or maybe not about it. Method overriding and overloading are two different concepts.

For example, in Java it is perfectly acceptable to have two methods that have exactly same name but differ by their parameter types. For example,

public void print( int param) {}
public void print( double param) {}

The names of the methods are identical. However, their signatures are not. This is an example of overloading because the method name, "print," is overloaded to take different types. You can also do:

public void print( int param1, float param2, String param3) {}

However, this is not possible in Objective-C. You cannot have two methods such as these:

-(void)print:(int)param;
-(void)print:(float)param;

Both of these methods have the name "print:" The compiler goes nuts:

gcc test.m -o test -l objc

test.m:6: error: duplicate declaration of method '-print:'
test.m:14: error: conflicting types for '-[Test print:]'
test.m:11: error: previous definition of '-[Test print:]' was here
test.m: In function '-[Test print:]':
test.m:14: warning: conflicting types for '-(void)print:(float)param'
test.m:5: warning: previous declaration of '-(void)print:(int)param'

Overloading on the other hand, involves redefining a method that exists in a superclass. This is supported in both Java and Objective-C (and most but not all OOP languages). In fact, it is easy to accidentally overload a method from a superclass when you meant to override it. For example, if you don't remember correctly what the parameter is in the base class:

public class A {
public void print( int param) {}
}

public class B extends A {
public void print( float param) {} // I meant to override but I actually overloaded because now B has print( int) and print( float).
}

I believe not being able to overload has something to do with dynamic typing but I'm not certain. Ruby, for example, has dynamic typing and forbids overloading. JavaScript is another example (although curiously, JavaScript doesn't flag this as an error. From what I've seen, it uses the last definition encountered although some say it uses them indeterminately).

To continue the weirdness, Objective-C, Java and Ruby all have different namespaces for variables and methods while C++ which permits overloading and overriding and is statically typed, does not.

public int numerator() { return numerator; }
public void setNumerator( int numerator) { this.numerator = numerator; }
private int numerator;

is possible in Java (and something similar is possible in Objective-C) but it is not possible in C++.

PowerBook G4 Mac OS X (10.4.7)

Jul 18, 2006 2:06 AM in response to cod3po37

I agree with you except because overloading means "multiple functions taking different types to be defined with the same name" where overriding "is a language feature that allows a subclass to provide a specific implementation of a method that is already provided by one of its superclasses."

iBook Mac OS X (10.4.7)

iBook Mac OS X (10.4.6)

Method overload in Objective-C

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