Junior — Middle
Is it possible to define multiple constructors with the same signature in a class?
sobes.tech AI
Answer from AI
No, in Java it is not possible to define multiple constructors with the same signature within a single class.
The constructor signature includes the types and order of parameters. If two constructors have the same parameter types in the same order, the compiler will produce an error due to ambiguity.
For example, the following code will cause an error:
public class Example {
public Example(int a, String b) {}
public Example(int x, String y) {} // Error: duplicate signature
}
To overload constructors, their signatures must differ in the number or types of parameters.