Which of these can be used to differentiate two or more methods having same name?
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. This set of Java Questions and Answers for Entrance exams on “Methods Taking Parameters”. 1. Which of these is the method which is executed first before execution of any other thing takes place in a program? Answer: c 2. What is the process of defining more than one method in a class differentiated by parameters?
Answer: b 3. Which of these can be used to differentiate two or more methods having the same name? Answer: d 4. Which of these data type can be used for a method having a return statement in it? Answer: d 5. Which of these statement is incorrect? Answer: d 6. What will be the output of the following Java program?
a) 0 Answer: c $ Prameterized_method.java $ Prameterized_method 6 7. What will be the output of the following Java program?
a) false Answer: b $ javac Output.java $ java Output true 8. What will be the output of the following Java program?
a)
0 Answer: b $ javac Output.java $ java Output 5 9. What will be the output of the following Java program?
a) 1 Answer: d $ javac Output.java Error: Main method not found in class Output, please define the main method as: public static void main(String[] args) 10. What will be the output of the following Java program? Can you have two methods with the same name?Two methods may share the same name, provided the number of parameters are different, or if they both have the same parameters, then there is at least one position, i where the parameter types differ.
What is the correct name of a method which has the same name?The practice of defining more than one method in a class with same name is called method overloading.
Can we have multiple methods with the same name in the same class?Yes, we can define multiple methods in a class with the same name but with different types of parameters. Which method is to get invoked will depend upon the parameters passed.
Which of the following is a method having same name as that of its class?Which of the following is a method having same name as that of it's class? Explanation: A constructor is a method that initializes an object immediately upon creation. It has the same name as that of class in which it resides.
|