What is the process of defining more than one method in a class having the same name but differentiated by method signature?
Here is an example of a typical method declaration: 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, (), and a body between braces, {}. 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, drawString, drawInteger, drawFloat, and so on. In the Java programming language, you can use the same name for all the drawing methods but pass a different argument list to each method. Thus, the data drawing class might declare four methods named draw, each of which has a different parameter list. 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, draw(String s) and draw(int i) are distinct and unique methods because they require different argument types. 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.
View Discussion Improve Article Save Article Like Article
View Discussion Improve Article Save Article Like Article
Method overloading in java is based on the number and type of the parameters passed as an argument to the methods. We can not define more than one method with the same name, Order, and type of the arguments. It would be a compiler error. The compiler does not consider the return type while differentiating the overloaded method. But you cannot declare two methods with the same signature and different return types. It will throw a compile-time error. If both methods have the same parameter types, but different return types, then it is not possible. Java can distinguish the methods with different method signatures. i.e. the methods can have the same name but with different parameters list (i.e. the number of the parameters, the order of the parameters, and data types of the parameters) within the same class. Parameters should be different means
2. Number of parameter should be different
Geeks, now you would be up to why do we need method overloading? If we need to do some kind of operation in different ways i.e. for different inputs. In the example described below, we are doing the addition operation for different inputs. It is hard to find many meaningful names for a single action. Ways of Overloading MethodsMethod overloading can be done by changing:
Let us propose examples in order to illustrate each way while overloading methods. They are as follows: Method 1: By changing the number of parameters.
Output sum of the two integer value :3 sum of the three integer value :6 Method 2: By changing the Data types of the parameters
Output sum of the three integer value :6 sum of the three double value :6.0 Method 3: By changing the Order of the parameters
Output geekName :Mohit Id :1 geekName :shubham Id :2
Example 4
Output: Related Articles: This article is contributed by Nitsdheerendra. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks. |