What is the process of defining two or more methods within the same class that have the same name but different parameters declaration?

Educative Answers Team

Overloading occurs when two or more methods in one class have the same method name but different parameters.

Overriding occurs when two methods have the same method name and parameters. One of the methods is in the parent class, and the other is in the child class. Overriding allows a child class to provide the specific implementation of a method that is already present in its parent class.​

The two examples below illustrate their differences:

The table below highlights their key differences:

  • Must have at least two methods by the same name in the class.

  • Must have a different number of parameters.

  • If the number of parameters is the same, then it must have different types of parameters.

  • Overloading is known as compile-time polymorphism.

  • Must have at least one method by the same name in both parent and child classes.

  • Must have the same number of parameters.

  • Must have the same parameter types.

  • Overriding is known as runtime polymorphism​.

Take a look at the code below:

class Dog{

public void bark[]{

System.out.println["woof "];

}

}

class Hound extends Dog{

public void sniff[]{

System.out.println["sniff "];

}

public void bark[]{

System.out.println["bowl"];

}

}

class OverridingTest{

public static void main[String [] args]{

Dog dog = new Hound[];

dog.bark[];

}

}

In this overriding example, the dog variable is declared to be a Dog. During compile-time, the compiler checks if the Dog class has the bark[] method. As long as the Dog class has the bark[] method, the code compiles. At run-time, a Hound is created and assigned to dog, so, ​ it calls the bark[] method of Hound.

Take a look at the code below:

class Dog{

public void bark[]{

System.out.println["woof "];

}

//overloading method

public void bark[int num]{

for[int i=0; i 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:

  • Have your method names describe what the method does clearly, without using too much jargon and abbreviations. A list of acceptable abbreviations is in the Coding Guidelines.

  • Have each parameter name describe the parameter and its purpose. On a method with exactly three parameters, you can use the word and to start the name of the last parameter if the method is programmed to perform two separate actions. In any other case, refrain from using and to start a parameter name. An example of the name of a method that performs two actions and uses the word and in its name is prefixFirstName:withInitials:andMakeInitialisUppercase:, where the method can prefix a first name [of type NSString] with the initials [of type NSString again] of that individual. In addition, the method accepts a boolean parameter named andMakeInitialsUppercase which, if set to YES, will prefix the first name with an uppercase equivalent of the initials passed to the method. If this parameter is set to NO, the method will use the initials it is given, without changing their case, to prefix the first name parameter.

  • Start method names with a lowercase letter.

  • For delegate methods, start the method name with the name of the class that invokes that delegate method.

Get iOS 6 Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.

Chủ Đề