Show
1.14. Defining Two or More Methods with the Same NameProblemYou would like to implement two or more methods with the same name in one object. In object-oriented programming, this is called method overloading. However, in Objective-C, method overloading does not exist in the same way as it does in other programming languages such as C++. SolutionUse the same name for your method, but keep the number and/or the names of your parameters different in every method: - (void) drawRectangle{ [self drawRectangleInRect:CGRectMake(0.0f, 0.0f, 4.0f, 4.0f)]; } - (void) drawRectangleInRect:(CGRect)paramInRect{ [self drawRectangleInRect:paramInRect withColor:[UIColor blueColor]]; } - (void) drawRectangleInRect:(CGRect)paramInRect withColor:(UIColor*)paramColor{ [self drawRectangleInRect:paramInRect withColor:paramColor andFilled:YES]; } - (void) drawRectangleInRect:(CGRect)paramInRect withColor:(UIColor*)paramColor andFilled:(BOOL)paramFilled{ /* Draw the rectangle here */ }This example shows a typical pattern in overloading. Each rectangle can be drawn either filled (solid color) or empty (showing just its boundaries). The first procedure is a “convenience procedure” that allows the caller to avoid specifying how to fill the rectangle. In our implementation of the first procedure, we merely call the second procedure, making the choice for the caller (andFilled:YES) The second procedure gives the caller control over filling.
DiscussionYou can define two methods with the same name so long as they differ in the parameters they accept. One reasons for doing this is one function offers more customization (through parameterization) than the other function. Method overloading is a programming language feature supported by Objective-C, C++, Java, and a few other languages. Using this feature, programmers can create different methods with the same name, in the same object. However, method overloading in Objective-C differs from that which can be used in C++. For instance, in C++, to overload a method, the programmer needs to assign a different number of parameters to the same method and/or change a parameter’s data type. In Objective-C, however, you simply change the name of at least one parameter. Changing the type of parameters will not work: - (void) method1:(NSInteger)param1{ /* We have one parameter only */ } - (void) method1:(NSString *)param1{ /* This will not compile as we already have a method called [method1] with one parameter */ }Changing the return value of these methods will not work either: - (int) method1:(NSInteger)param1{ /* We have one parameter only */ return param1; } - (NSString *) method1:(NSString *)param1{ /* This will not compile as we already have a method called [method1] with one parameter */ return param1; }As a result, you need to change the number of parameters or the name of (at least) one parameter that each method accepts. Here is an example where we have changed the number of parameters: - (NSInteger) method1:(NSInteger)param1{ return param1; } - (NSString*) method1:(NSString *)param1 andParam2:(NSString *)param2{ NSString *result = param1; if ([param1 length] > 0 && [param2 length] > 0){ result = [result stringByAppendingString:param2]; } return result; }Here is an example of changing the name of a parameter: - (void) drawCircleWithCenter:(CGPoint)paramCenter radius:(CGFloat)paramRadius{ /* Draw the circle here */ } - (void) drawCircleWithCenter:(CGPoint)paramCenter Radius:(CGFloat)paramRadius{ /* Draw the circle here */ }Can you spot the difference between the declarations of these two methods? The first method’s second parameter is called radius (with a lowercase r) whereas the second method’s second parameter is called Radius (with an uppercase R). This will set these two methods apart and allows your program to get compiled. However, Apple has guidelines for choosing method names as well as what to do and what not to do when constructing methods. For more information, please refer to the “Coding Guidelines for Cocoa” Apple documentation available here. Here is another example of two methods that draw a circle but have different names for their second parameter: - (void) drawCircleWithCenter:(CGPoint)paramCenterPoint radiusInPoints:(CGFloat)paramRadiusInPoints{ /* Draw the circle here */ } - (void) drawCircleWithCenter:(CGPoint)paramCenterPoint radiusInMillimeters:(CGFloat)paramRadiusInMillimeters{ /* Draw the circle here */ }Here is a concise extract of the things to look out for when constructing and working with methods:
What is the process of defining two or more methods within the same class having same name but different parameters?The practice of defining two or more methods within the same class that share the same name but have different parameters is called overloading methods.
What is the process of defining two or more methods within same class that have same name but different parameters declaration a method overloading?In Java, two or more methods may have the same name if they differ in parameters (different number of parameters, different types of parameters, or both). These methods are called overloaded methods and this feature is called method overloading.
What is the process of defining two or more methods within same class that have same name but different parameters declaration in Java?If a class has multiple methods having same name but parameters of the method should be different is known as Method Overloading.
What is the process of defining more than one method in a class having the same name but differentiated by method signature?Explanation: Function overloading is a process of defining more than one method in a class with same name differentiated by function signature i:e return type or parameters type and number.
|