As one of my constructors, I used the following:
public Human (int weight) {
this.weight = weight;
}
I received the message below. I changed the parameters of the constructor to something else and it compiled and verified. What was it trying to tell me in the error message? I don't know why my change worked. Thanks for your help.
com/codegym/task/task10/task1013/Solution.java:52: error: constructor Human(int) is already defined in class com.codegym.task.task10.task1013.Solution.Human
public Human(int age) {
Explanation of why it wouldn't compile this way
Under discussion
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
Aleksandar
22 August 2019, 00:34
Well i don't know how to explain it to you. You have two constructors with the same constructor signature, so i this case
Human(int age) and,
Human(int weight)
And if you create an object Human(45);
So the compiler can't determinate which constructor to call the one for weight or the one for age...
So it's impossible to have constructors with same signatures.
I hope you understand me, if you don't then google Constructor overloading.
0
Neil Hainer
8 December 2020, 19:48
I think your explanation was very good.
0