Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is function overloading possible in Objective C?

Is function overloading possible in Objective C ?
Well,Most of the programmers says no,
But it looks like possible,
for example:

-(int)AddMethod:(int)X :(int)Y
{
    return X + Y;
}
-(int)AddMethod:(int)X
{
    return X;
}

to call 1st one write [self AddMethod :3];
to call last one write [self AddMethod: 3 :4];

like image 914
Matrix Avatar asked Sep 08 '10 09:09

Matrix


People also ask

Can we overload functions in OOP?

Method overloading is a salient feature in Object-Oriented Programming (OOP). It lets you declare the same method multiple times with different argument lists. In this guide, we are going to discuss how we can achieve method overloading in C#.

Is overloading is possible in C?

Function overloading is a feature of Object Oriented programming languages like Java and C++. As we know, C is not an Object Oriented programming language. Therefore, C does not support function overloading.

What are the limitations of function overloading?

In function overloading, the main disadvantage is that the functions with different return types can't be overloaded. In the case of a static function, the same parameters cannot be overloaded.

What is function overloading in OOP with example?

Function overloading is a feature of object-oriented programming where two or more functions can have the same name but different parameters. When a function name is overloaded with different jobs it is called Function Overloading.


2 Answers

Method overloading is not possible in Objective-C. However, your example actually will work because you have created two different methods with different selectors: -AddMethod:: and AddMethod:. There is a colon for each interleaved parameter. It's normal to put some text in also e.g. -addMethodX:Y: but you don't have to.

like image 157
JeremyP Avatar answered Oct 07 '22 12:10

JeremyP


No, it is not, mostly because Objective-C doesn't use functions, it uses methods.

Method overloading, on the other hand, is possible. Sort of.

Consider, if you will, a class with a method take an argument on the form of either an NSString * or a const char *:

@interface SomeClass : NSObject {

}

- (void)doThingWithString:(NSString *)string;
- (void)doThingWithBytes:(const char *)bytes;

@end

While the method itself won't go around choosing the proper method with a given input; one could still say that doThing: was overloaded, at least in the sense that the two methods taking a different parameter to achieve the same functionality.

like image 26
Williham Totland Avatar answered Oct 07 '22 13:10

Williham Totland