Can a class have 2 methods with the same name?

We can have a method name same as a class name in Java but it is not a good practice to do so. This concept can be clear through example rather than explanations. In the below example, a default constructor is called when an object is created and a method with the same name is called using obj.Main().

Example 1:

Java




// Java program to demonstrate that a method

// can have same name as a Constructor or

// class name

 

import java.io.*;

 

public

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
0
error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
1

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
2
error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
3
error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
4

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
2
error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
6

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
7
error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
8

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
9
5
0
5
1

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
2
5
3

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
2public
5
6
error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
3
5
8

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
2
error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
6

Hey
1
Hey
2

Hey
1
Hey
4

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
7
Hey
6
Hey
7
Hey
8

Hey

Hey
1// Java program to demonstrate that a method1

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
7// Java program to demonstrate that a method3

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
2
5
3

5
3

Output

My name is same as Constructor name!

Example 2: To check whether it’s really showing constructor property or not we can check like this.

Java




// Java program to demonstrate that a method7

// Java program to demonstrate that a method8

// Java program to demonstrate that a method9

 

import java.io.*;

 

public

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
0
error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
1

 

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
2// can have same name as a Constructor or6

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
2// can have same name as a Constructor or8// can have same name as a Constructor or9// class name0// class name1// class name2

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
2// class name4

// class name5// class name6

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
7// class name8

// class name5

5
3

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
2import2

 

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
2import4

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
2import6

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
2
error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
3 import9java.io.*;0 java.io.*;1

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
2
error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
6

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
7
error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
8java.io.*;6
5
1

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
2
5
3

public

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
2public
5
6
error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
3
5
8

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
2
error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
6

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
7public9

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
7
Hey
6
Hey
7
Hey
8

Hey

Hey
1
error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
06

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
2
5
3

5
3

Error:

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error

 

Here we don’t call the void Main(int) through an object, but we call through this(int) and its showing error so this cannot be a constructor. This is going to give you an error showing that no parameterized constructor is available, but we think that we have it already that is void Main(int a). But void Main(int a) is not acting like that to prove that just remove the commented section in above code then only it’s going to work.

Java




error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
10

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
11

 

import java.io.*;

 

public

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
0
error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
1

 

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
2// can have same name as a Constructor or6

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
2// can have same name as a Constructor or8// can have same name as a Constructor or9// class name0// class name1// class name2

 

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
2
error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
26

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
2import9java.io.*;0
error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
30

 

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
2
error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
32

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
2
error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
3 import9java.io.*;0 java.io.*;1

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
2
error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
6

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
7
error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
8

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
9
error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
43
5
1

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
2
5
3

 

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
2public
5
6
error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
3
5
8

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
2
error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
6

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
7
Hey
6
Hey
7
Hey
8

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
7
error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
06

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
2
5
3

5
3

Output

5

Example 3: 

Java




// Java program to demonstrate that a method7

// Java program to demonstrate that a method8

// Java program to demonstrate that a method9

 

import java.io.*;

 

public

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
0
error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
1

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
2// can have same name as a Constructor or6

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
2
error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
74
error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
75// class name2

 

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
2
error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
78

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
2
error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
3
error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
4

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
2
error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
6

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
7
error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
8
error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
86
5
1

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
2
5
3

 

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
2public
5
6
error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
3
5
8

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
2
error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
6

 

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
7public9

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
7
Hey
6
Hey
7
Hey
8

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error
2
5
3

5
3

Output

Hey

Finally, we can conclude that when we have a return type for the methods with the name same as a class name then that loses the features the constructors hold and that will behave like a method. And this is a bad practice in programming.

Can a class have methods with the same name?

Yes, It is allowed to define a method with the same name as that of a class. There is no compile-time or runtime error will occur.

Can two methods of a class have the same name and the same signature?

Method Signature is the combination of a method's name and its parameter list. A class cannot have two methods with the same signature.

When a class has more than one method with the same name?

The practice of defining more than one method in a class with same name is called method overloading.

Is it possible to have two methods within the same class that have the same name and parameter lists but which have different return types?

Method overloading means two or more methods have the same name but have different parameter lists: either a different number of parameters or different types of parameters.