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.
If a class has multiple methods having same name but different in parameters, it is known as Method Overloading. If we have to perform only one operation, having same name of the methods increases the readability of the program. Suppose you have to perform addition of the given numbers but there can be any number of arguments, if you write the method such as a(int,int) for two parameters, and b(int,int,int) for three parameters then it may be difficult for you as well as other programmers to understand the behavior of the method because its name differs. So, we perform method overloading to figure out the program quickly. Advantage of method overloadingMethod overloading increases the readability of the program. Different ways to overload the methodThere are two ways to overload the method in java
In Java, Method Overloading is not possible by changing the return type of the method only.1) Method Overloading: changing no. of argumentsIn this example, we have created two methods, first add() method performs addition of two numbers and second add method performs addition of three numbers. In this example, we are creating static methods so that we don't need to create instance for calling methods. Test it NowOutput: 2) Method Overloading: changing data type of argumentsIn this example, we have created two methods that differs in data type. The first add method receives two integer arguments and second add method receives two double arguments. Test it NowOutput: Q) Why Method Overloading is not possible by changing the return type of method only?In java, method overloading is not possible by changing the return type of the method only because of ambiguity. Let's see how ambiguity may occur: Test it NowOutput: Compile Time Error: method add(int,int) is already defined in class Adder System.out.println(Adder.add(11,11)); //Here, how can java determine which sum() method should be called? Note: Compile Time Error is better than Run Time Error. So, java compiler renders compiler time error if you declare the same method having same parameters.Can we overload java main() method?Yes, by method overloading. You can have any number of main methods in a class by method overloading. But JVM calls main() method which receives string array as arguments only. Let's see the simple example: Test it NowOutput: Method Overloading and Type PromotionOne type is promoted to another implicitly if no matching datatype is found. Let's understand the concept by the figure given below: As displayed in the above diagram, byte can be promoted to short, int, long, float or double. The short datatype can be promoted to int, long, float or double. The char datatype can be promoted to int,long,float or double and so on. Example of Method Overloading with TypePromotionTest it NowExample of Method Overloading with Type Promotion if matching foundIf there are matching type arguments in the method, type promotion is not performed. Test it NowOutput:int arg method invoked Example of Method Overloading with Type Promotion in case of ambiguityIf there are no matching type arguments in the method, and each method promotes similar number of arguments, there will be ambiguity. Test it NowOutput:Compile Time Error One type is not de-promoted implicitly for example double cannot be depromoted to any type implicitly.Can you have two methods in a class with the same method signature but different return types yes?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.
Can we have same method name with different return type?No, you cannot overload a method based on different return type but same argument type and number in java. same name.
Can return types affect method signatures?According to Oracle, the method signature is comprised of the name and parameter types. Therefore, all the other elements of the method's declaration, such as modifiers, return type, parameter names, exception list, and body are not part of the signature.
Can a class have two methods with the same name?Yes, we can define multiple methods in a class with the same name but with different types of parameters.
|