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—such as
public
,private
, and others you will learn about later. - The return type—the data type of the value returned by the method, or
void
if the method does not return a value. - The method name—the rules for field names apply to method names as well, but the convention is a little different.
- The parameter list in parenthesis—a comma-delimited list of input parameters, preceded by their data types, enclosed by parentheses,
[]
. If there are no parameters, you must use empty parentheses. - An exception list—to be discussed later.
- The method body, enclosed between braces—the method's code, including the declaration of local variables, goes here.
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 Method
Although 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 Methods
The 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,
calculateAnswer[double, int, double, double]0,
calculateAnswer[double, int, double, double]1,
calculateAnswer[double, int, double, double]2, 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
calculateAnswer[double, int, double, double]3, 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,
calculateAnswer[double, int, double, double]4 and
calculateAnswer[double, int, double, double]5 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.
For explanation I would say: Two or more methods can have same name as long as their parameters declaration is different, the methods are said to be overloaded and process is called method overloading. Method overloading is a way by which Java implements polymorphism.
Two or more methods can have same name as long as their parameters declaration is different, the methods are said to be overloaded and process is called method overloading. Method overloading is a way by which Java implements polymorphism.
This section of our 1000+ Java MCQs focuses on overloading methods & argument passing in Java Programming Language.
1. What is the process of defining two or more methods within same class that have same name but different parameters declaration?
a] method overloading
b] method overriding
c] method hiding
d] none of the mentioned
View Answer
Answer: a
Explanation: Two or more methods can have same name as long as their parameters declaration is different, the methods are said to be overloaded and process is called method overloading. Method overloading is a way by which Java implements polymorphism.
2. Which of these can be overloaded?
a] Methods
b] Constructors
c] All of the mentioned
d] None of the mentioned
View Answer
Answer: c
Explanation: None.
3. Which of these is correct about passing an argument by call-by-value process?
a] Copy of argument is made into the formal parameter of the subroutine
b] Reference to original argument is passed to formal parameter of the subroutine
c] Copy of argument is made into the formal parameter of the subroutine and changes made on parameters of subroutine have effect on original argument
d] Reference to original argument is passed to formal parameter of the subroutine and changes made on parameters of subroutine have effect on original argument
View Answer
Answer: a
Explanation: When we pass an argument by call-by-value a copy of argument is made into the formal parameter of the subroutine and changes made on parameters of subroutine have no effect on original argument, they remain the same.
Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement
4. What is the process of defining a method in terms of itself, that is a method that calls itself?
a] Polymorphism
b] Abstraction
c] Encapsulation
d] Recursion
View Answer
Answer: d
Explanation: None.
5. What will be the output of the following Java code?
Take Java Programming Practice Tests - Chapterwise!
Start the Test Now: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
class San
{
public void m1 [int i,float f]
{
System.out.println[" int float method"];
}
public void m1[float f,int i];
{
System.out.println["float int method"];
{
0{
2{
{
4{
5{
0{
7
a] int float method
b] float int method
c] compile time error
d] run time error
View Answer
Answer: c
Explanation: While resolving overloaded method, compiler automatically promotes if exact match is not found. But in this case, which one to promote is an ambiguity.
advertisement
6. What will be the output of the following Java code?
{
8{
9public void m1 [int i,float f]
0public void m1 [int i,float f]
1public void m1 [int i,float f]
2public void m1 [int i,float f]
3public void m1 [int i,float f]
4public void m1 [int i,float f]
5public void m1 [int i,float f]
6public void m1 [int i,float f]
3public void m1 [int i,float f]
8public void m1 [int i,float f]
9{
0{
1{
9{
3public void m1 [int i,float f]
3{
5{
6{
7{
8public void m1 [int i,float f]
5System.out.println[" int float method"];
0
a] 5
b] 6
c] 7
d] 8
View Answer
Answer: c
Explanation: None.
output:
advertisement
System.out.println[" int float method"];1
7. What will be the output of the following Java code?
{
8{
9public void m1 [int i,float f]
0public void m1 [int i,float f]
1System.out.println[" int float method"];
6public void m1 [int i,float f]
3public void m1 [int i,float f]
4public void m1 [int i,float f]
5}
0public void m1 [int i,float f]
3public void m1 [int i,float f]
8public void m1 [int i,float f]
9{
0{
1{
9{
3public void m1 [int i,float f]
3{
5{
6{
8public void m1 [int i,float f]
5
a] 6
b] 7
c] 8
d] 9
View Answer
Answer: c
Explanation: None.
output:
5
8. What will be the output of the following Java code?
public void m1 [int i,float f]
0public void m1[float f,int i];
0public void m1 [int i,float f]
3public void m1[float f,int i];
2public void m1 [int i,float f]
5public void m1[float f,int i];
4public void m1 [int i,float f]
3public void m1[float f,int i];
6public void m1 [int i,float f]
5public void m1[float f,int i];
8public void m1 [int i,float f]
3{
0{
1public void m1 [int i,float f]
9{
0{
1{
9{
3public void m1 [int i,float f]
3{
5{
9System.out.println["float int method"];
0System.out.println["float int method"];
1System.out.println["float int method"];
2System.out.println["float int method"];
3public void m1 [int i,float f]
5System.out.println[" int float method"];
0
a] 6 6
b] 6.4 6.4
c] 6.4 6
d] 4 6.4
View Answer
Answer: d
Explanation: For obj.add[a,a]; ,the function in line number 4 gets executed and value of x is 4. For the next function call, the function in line number 7 gets executed and value of y is 6.4
output:
System.out.println["float int method"];6
9. What will be the output of the following Java code?
System.out.println["float int method"];
7{
9System.out.println["float int method"];
9{
00{
01public void m1 [int i,float f]
3{
03{
04{
05{
0{
07{
9{
3public void m1 [int i,float f]
3{
11{
12{
13{
14{
15{
16
a] 10 20
b] 20 10
c] 20 40
d] 40 20
View Answer
Answer: a
Explanation: Variables a & b are passed by value, copy of their values are made on formal parameters of function meth[] that is i & j. Therefore changes done on i & j are not reflected back on original arguments. a & b remain 10 & 20 respectively.
output:
{
1810. What will be the output of the following Java code?
System.out.println["float int method"];
7{
9System.out.println["float int method"];
9{
00{
23public void m1 [int i,float f]
3{
25{
26public void m1 [int i,float f]
5{
28public void m1 [int i,float f]
3{
30{
31{
05{
0{
07{
9{
3public void m1 [int i,float f]
3{
38{
39{
40{
16
a] 10 20
b] 20 10
c] 20 40
d] 40 20
View Answer
Answer: b
Explanation: Class objects are always passed by reference, therefore changes done are reflected back on original arguments. obj.meth[obj] sends object obj as parameter whose variables a & b are multiplied and divided by 2 respectively by meth[] function of class test. a & b becomes 20 & 10 respectively.
output:
{
43Sanfoundry Global Education & Learning Series – Java Programming Language.
To practice all areas of Java language, here is complete set of 1000+ Multiple Choice Questions and Answers.