What is the process of defining two or more methods within the same class

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:

  1. Modifiers—such as public, private, and others you will learn about later.
  2. The return type—the data type of the value returned by the method, or void if the method does not return a value.
  3. The method name—the rules for field names apply to method names as well, but the convention is a little different.
  4. 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.
  5. An exception list—to be discussed later.
  6. 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.

What is the process of defining two or more methods within the same class

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

  1. class San
  2. {
  3.  public void m1 (int i,float f)
  4.  {
  5.   System.out.println(" int float method");
  6.  }
  7.  
  8.  public void m1(float f,int i);
  9.   {
  10.   System.out.println("float int method");
  11. {
    0
  12.  
  13. {
    2
  14.   {
  15. {
    4
  16. {
    5
  17. {
    0
  18. {
    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?

  1. {
    8
  2. {
    9
  3.  public void m1 (int i,float f)
    0
  4.  public void m1 (int i,float f)
    1
  5.  public void m1 (int i,float f)
    2
  6.  public void m1 (int i,float f)
    3
  7.  public void m1 (int i,float f)
    4
  8.  public void m1 (int i,float f)
    5
  9.  public void m1 (int i,float f)
    6
  10.  public void m1 (int i,float f)
    3
  11.  public void m1 (int i,float f)
    8
  12.  public void m1 (int i,float f)
    9
  13.  {
    0
  14.  {
    1
  15. {
    9
  16.  {
    3
  17.  public void m1 (int i,float f)
    3
  18.  {
    5
  19.  {
    6
  20.  {
    7
  21.  {
    8
  22.  public void m1 (int i,float f)
    5
  23.   System.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?

  1. {
    8
  2. {
    9
  3.  public void m1 (int i,float f)
    0
  4.  public void m1 (int i,float f)
    1
  5.   System.out.println(" int float method");
    6
  6.  public void m1 (int i,float f)
    3
  7.  public void m1 (int i,float f)
    4
  8.  public void m1 (int i,float f)
    5
  9.  }
    0
  10.  public void m1 (int i,float f)
    3
  11.  public void m1 (int i,float f)
    8
  12.  public void m1 (int i,float f)
    9
  13.  {
    0
  14.  {
    1
  15. {
    9
  16.  {
    3
  17.  public void m1 (int i,float f)
    3
  18.  {
    5
  19.  {
    6
  20.  
    1
  21.  {
    8
  22.  public void m1 (int i,float f)
    5
  23.  
    4

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?

  1.  
    6
  2.  
    7
  3.  public void m1 (int i,float f)
    0
  4.  
    9
  5.  public void m1(float f,int i);
    0
  6.  public void m1 (int i,float f)
    3
  7.  public void m1(float f,int i);
    2
  8.  public void m1 (int i,float f)
    5
  9.  public void m1(float f,int i);
    4
  10.  public void m1 (int i,float f)
    3
  11.  public void m1(float f,int i);
    6
  12.  public void m1 (int i,float f)
    5
  13.  public void m1(float f,int i);
    8
  14.  public void m1 (int i,float f)
    3
  15.   {
    0
  16.   {
    1
  17.  public void m1 (int i,float f)
    9
  18.  {
    0
  19.  {
    1
  20. {
    9
  21.  {
    3
  22.  public void m1 (int i,float f)
    3
  23.  {
    5
  24.   {
    9
  25.   System.out.println("float int method");
    0
  26.   System.out.println("float int method");
    1
  27.   System.out.println("float int method");
    2
  28.   System.out.println("float int method");
    3
  29.  public void m1 (int i,float f)
    5
  30.   System.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?

  1.   System.out.println("float int method");
    7
  2. {
    9
  3.   System.out.println("float int method");
    9
  4. {
    00
  5. {
    01
  6.  public void m1 (int i,float f)
    3
  7. {
    03
  8. {
    04
  9. {
    05
  10.  {
    0
  11. {
    07
  12. {
    9
  13.  {
    3
  14.  public void m1 (int i,float f)
    3
  15. {
    11
  16. {
    12
  17. {
    13
  18. {
    14
  19. {
    15
  20. {
    16
  21.  
    4

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:

{
18

10. What will be the output of the following Java code?

  1.   System.out.println("float int method");
    7
  2. {
    9
  3.   System.out.println("float int method");
    9
  4. {
    00
  5. {
    23
  6.  public void m1 (int i,float f)
    3
  7. {
    25
  8. {
    26
  9.  public void m1 (int i,float f)
    5
  10. {
    28
  11.  public void m1 (int i,float f)
    3
  12. {
    30
  13. {
    31
  14. {
    05
  15.  {
    0
  16. {
    07
  17. {
    9
  18.  {
    3
  19.  public void m1 (int i,float f)
    3
  20. {
    38
  21. {
    39
  22. {
    40
  23. {
    16
  24.  
    4

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:

{
43

Sanfoundry 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.

What is the process of defining more than one method in a class with the same name?

Explanation: Function overloading is a process of defining more than one method in a class with same name differentiated by function signature i:e return type or parameters type and number.

What is the process of defining a method in a subclass having same?

Explanation: When a method in a subclass has the same name and type signatures as a method in the superclass, then the method in the subclass overrides the method in the superclass.

What is the process of defining a method in terms of itself?

Recursion is the process of defining something in terms of itself. Recursion is sometimes called circular definition. A function that calls itself is said to be recursive.