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(). Show
Example 1: 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 error0 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 error1 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 error2 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 error3 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 error4 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 error2 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 error6 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 error7 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 error8 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 error9 50 51 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 error2 53 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 error2 public 56 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 error3 58 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 error2 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 error6 Hey1 Hey2 Hey1 Hey4 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 error7 Hey6 Hey7 Hey8 Hey1 Hey1 // Java program to demonstrate that a method 1error: 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 error7 // Java program to demonstrate that a method 3error: 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 error2 53 53 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
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 error0 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 error1
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 error2 // can have same name as a Constructor or 6error: 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 error2 // can have same name as a Constructor or 8// can have same name as a Constructor or 9// class name 0// class name 1// class name 2error: 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 error2 // class name 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 error7 // class name 8
53 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 error2 import 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 error2 import 4error: 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 error2 import 6error: 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 error2 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 error3 import 9java.io.*; 0 java.io.*; 1error: 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 error2 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 error6 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 error7 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 error8 java.io.*; 651 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 error2 53
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 error2 public 56 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 error3 58 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 error2 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 error6 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 error7 public 9error: 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 error7 Hey6 Hey7 Hey8 Hey1 Hey1 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 error06 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 error2 53 53 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. Javaerror: 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 error10 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 error11
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 error0 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 error1
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 error2 // can have same name as a Constructor or 6error: 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 error2 // can have same name as a Constructor or 8// can have same name as a Constructor or 9// class name 0// class name 1// class name 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 error2 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 error26 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 error2 import 9java.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 error30
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 error2 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 error32 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 error2 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 error3 import 9java.io.*; 0 java.io.*; 1error: 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 error2 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 error6 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 error7 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 error8 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 error9 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 error43 51 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 error2 53
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 error2 public 56 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 error3 58 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 error2 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 error6 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 error7 Hey6 Hey7 Hey8 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 error7 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 error06 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 error2 53 53 Output 5 Example 3: 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 error0 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 error1 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 error2 // can have same name as a Constructor or 6error: 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 error2 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 error74 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 error75 // class name 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 error2 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 error78 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 error2 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 error3 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 error4 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 error2 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 error6 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 error7 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 error8 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 error86 51 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 error2 53
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 error2 public 56 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 error3 58 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 error2 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 error6
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 error7 public 9error: 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 error7 Hey6 Hey7 Hey8 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 error2 53 53 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.
|