Here is an example of a typical method declaration: Show
public double calculateAnswer(double wingSpan, int numberOfEngines, double length, double grossTons) { //do the calculation here } The only required elements of a method declaration are the method's return type, name, a pair of parentheses, More generally, method declarations have six components, in order:
Modifiers, return types, and parameters will be discussed later in this lesson. Exceptions are discussed in a later lesson. Definition: Two of the components of a method declaration comprise the method signature—the method's name and the parameter types. The signature of the method declared above is: calculateAnswer(double, int, double, double) Naming a MethodAlthough a method name can be any legal identifier, code conventions restrict method names. By convention, method names should be a verb in lowercase or a multi-word name that begins with a verb in lowercase, followed by adjectives, nouns, etc. In multi-word names, the first letter of each of the second and following words should be capitalized. Here are some examples: run runFast getBackground getFinalData compareTo setX isEmpty Typically, a method has a unique name within its class. However, a method might have the same name as other methods due to method overloading. Overloading MethodsThe Java programming language supports overloading methods, and Java can distinguish between methods with different method signatures. This means that methods within a class can have the same name if they have different parameter lists (there are some qualifications to this that will be discussed in the lesson titled "Interfaces and Inheritance"). Suppose that you have a class that can use calligraphy
to draw various types of data (strings, integers, and so on) and that contains a method for drawing each data type. It is cumbersome to use a new name for each method—for example, public class DataArtist { ... public void draw(String s) { ... } public void draw(int i) { ... } public void draw(double f) { ... } public void draw(int i, double f) { ... } } Overloaded methods are differentiated by the number and the type of the arguments passed into the method. In the code sample, You cannot declare more than one method with the same name and the same number and type of arguments, because the compiler cannot tell them apart. The compiler does not consider return type when differentiating methods, so you cannot declare two methods with the same signature even if they have a different return type. Note: Overloaded methods should be used sparingly, as they can make code much less readable. 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:
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 (
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:
Changing the return value of these methods will not work either:
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:
Here is an example of changing the name of a parameter:
Can you spot the difference between the declarations of
these two methods? The first method’s second parameter is called Here is another example of two methods that draw a circle but have different names for their second parameter:
Here is a concise extract of the things to look out for when constructing and working with methods:
What is process of defining two methods with the same method name and 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. Code section 4.22: Method overloading.
What is it called when two methods have the same name but different parameters?Method overloading means two or more methods have the same name but have different parameter lists: either a different number of parameters or different types of parameters. When a method is called, the corresponding method is invoked by matching the arguments in the call to the parameter lists of the methods.
What is it called when two methods have the same name?Having two or more methods named the same in the same class is called overloading.
When a method has the same name and takes the same parameters?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.
|