Java, compilation error, Constructors
Asked Answered
A

3

7

I have been trying a mock ocjp 6 test. I went though a question asking if the constructor is correct :

 1- public Test8(){}
 2- private void Test8(){}
 3- protected Test8(int k){}
 4- Test8(){}

The correct answer was 1 and 3. I didn't understand why the 4 was incorrect. When I tested the following code:

public class Test8 {
    Test8() {}

    public Test8() {}

}

I have compilation error, but when I remove one of the constructors if compile without any issue.

Someone can clear it up for me please.

Anthropomorphous answered 19/5, 2015 at 14:16 Comment(8)
1, 3 and 4 all look good to me. Are you sure that you transcribed the question fully and correctly?Aggress
javabeat.net/ocpjp-6-mock-exam-java-1 question 8, check the answer and copy/past the class and try itAnthropomorphous
the compilation error is because the constructors are the same, if you call one, the computer can't see which one you want to use. if you want to check, test everyone separately.Wore
They don't ask if you can use them all at once, they only ask which are not valid.Lavernelaverock
The only problem with 4 that I can think of is that it is package private, meaning it cannot be used outside the package it is declared in, which is unusual but could be deliberate.Pennypennyaliner
yes Filip, I understand, I was just testing to understand I went through to duplicate issue, but now the question is, why they put Test8(){} alone as invalid, but it compile perfectly when tested, it must be error in the answer itselfAnthropomorphous
Perhaps the question intends for the constructors to be correct at the same time in the same class. Thus only 1 and 3 or 3 and 4 can be correct together. But given question 9 after it, I would go with "answer is incomplete".Emma
@oueslatibilel, I was replying to amit. :)Lavernelaverock
G
7

The confusing this about this stackoverflow question is that it's about another question. So when people answer referring to the "question" it's unclear which.


For your question about why this won't compile, it's because they both have the same signature (method name and params). Return type and visibility (public, private, protected) don't matter for making unique signatures.

public class Test8 {
    Test8() {}

    public Test8() {}
}

Because those both have the same name and parameter types they're the same method as far as the compiler is concerned, and that is why it worked when you removed one because it didn't have a duplicate.


As for the test question

Q8: Which of the following are valid Constructors?

  1. public Test8(){}
  2. private void Test8(){}
  3. protected Test8(int k){}
  4. Test8(){}

the only invalid one is 2 because it has a return type (void) listed. Constructors don't have return types. The site lists the correct answer as 1 and 3 though.

Q8:

  • 1 is correct. public Test8(){}.
  • 3 is correct. protected Test8(int k){}.

Why?

  • Possibly they put them all in a java file and tried to compile it like you and thought the 4 was invalid.
  • Maybe they think constructors need a visibility modifier?
  • Perhaps it's a terribly worded question and they meant "which of these can be used together starting from the top, ones below the cause the compilation to fail are incorrect"

No matter how you slice it, the question/answer on that site are poor.

Ghibelline answered 19/5, 2015 at 14:44 Comment(0)
Q
2

The only sensible answer is that the mock exam answer key is incorrect. 1, 3, and 4 are valid constructors. It might not be valid to have 1 and 4 as constructors for the same class but the question didn't ask that.

Querulous answered 19/5, 2015 at 14:16 Comment(0)
C
0

You cannot re-declare the same constructor with the same arguments(in this case no arguments)

You are defining first a private constructor. Then you are defining a public constructor but with the same amount of arguments.

You need to chose wether you want a private or public constructor.

public class Test8 {
    Test8() {}// Or this one

    public Test8() {} // Or this one. but not both.

}
Coastguardsman answered 19/5, 2015 at 14:20 Comment(4)
The question itself mentions nothing about multiple constructors.Aggress
yes you are right about that, I declared it package (not private) and public but no change of the arg, true. that answer the second question.Anthropomorphous
@everyone actually this does answer part of the question the way the question is worded is a little confusing as to what the op is actually asking...Quintal
Op asked what was wrong and why it wasn't correct. The reason is simple java rules. You cannot have two constructors/methods with the same amount of arguments. The entire question revolves about him wanting to understand why it fails. This is why his compilation errors occurred and why question 4 failed.Coastguardsman

© 2022 - 2024 — McMap. All rights reserved.