Junior
What is a default constructor?
sobes.tech AI
Answer from AI
Default constructor — is a constructor that is automatically generated by the Java compiler for a class if no explicit constructors are declared in that class.
Main characteristics:
- It does not take any arguments.
- It has the same visibility as the class (usually
public). - It calls the parent class's no-argument constructor (
super();).
Example:
class MyClass {
// The compiler will generate MyClass() {} automatically
// if there are no other constructors in the class
}
class AnotherClass {
int value;
// Explicit constructor, default constructor is not generated
public AnotherClass(int value) {
this.value = value;
}
public AnotherClass() {
// This is NOT a default constructor, this is an explicitly declared no-argument constructor
}
}