Interview Q&A · Java · 5 questions in this topic
Constructors: Rules, Types & Chaining — Java Interview Q&A
Constructors initialise new objects. They share the class name, have no return type, and chain with this() — or hand off to the superclass with super(). Part of Methods & Constructors · 294 questions in the Java bank.
How do you call one constructor from another constructor?
Answer
Use this(...) with the matching argument list. It must be the very first statement of the constructor, and a constructor can only make one such call — so this(...) and super(...) can never appear together in the same constructor.
Key points
- 1this(...) picks the overload whose parameter list matches the arguments.
- 2It must be the first statement; a second this(...) or super(...) in the same constructor is a compile error.
- 3If you write no explicit call, the compiler inserts super() (the no-arg superclass constructor) as the first statement.
Example
class Point { int x, y; Point() { this(0, 0); } // delegates to the 2-arg constructor Point(int x, int y) { this.x = x; this.y = y; } }
What is a constructor in Java?
Answer
A constructor is a special block that runs automatically when an object is created with new, initialising its fields. It has exactly the class name and no return type — not even void. A class may declare several constructors (overloading); if it declares none, the compiler supplies an invisible no-argument default constructor.
Key points
- 1Default constructor: compiler-provided, no-arg, does nothing extra. Disappears the moment you declare any constructor yourself.
- 2Parameterised constructor: takes arguments so callers can set initial state.
- 3Constructors are not methods: they are never inherited and never overridden.
Example
class Car { String model; Car() { this("Unknown"); } // default-style constructor Car(String model) { this.model = model; } // parameterised constructor }
If a class has a parameterised constructor, does the compiler still create a default constructor?
Answer
No. The compiler only generates a default no-arg constructor when the class declares no constructors at all. The moment you write any constructor — parameterised or explicit no-arg — that is the complete set, and new Foo() compiles only if a matching constructor exists.
Key points
- 1class Car {} → the compiler adds Car() automatically.
- 2class Car { Car(String m) {...} } → new Car() no longer compiles.
- 3A common interview bug: adding a parameterised constructor breaks framework or subclass code that relied on the invisible default.
Can a method have the same name as its class?
Answer
Yes, it compiles — a method with a return type is distinct from a constructor, which has no return type. But it is a notorious trap: readers mistake it for a constructor. Style checkers flag it, and interviews use it to test whether you know constructors are recognised by name-plus-no-return-type, not by name alone.
Example
class Car { void Car() { } // NOT a constructor — an ordinary method that happens to share the name }
Can constructors be overridden?
Answer
No. Only methods (instance methods, to be precise) are overridden, and constructors are not inherited at all — a subclass gets none of its parent’s constructors automatically. What looks similar is constructor chaining: a subclass constructor must call one of the superclass constructors via super(...) (explicitly, or the compiler-injected no-arg super()).
Key points
- 1Constructors are not members, so they cannot be inherited or overridden.
- 2They can be overloaded within the same class.
- 3A subclass that declares no constructor still gets a default one that calls super().
Learn this topic
Recommended resourcesAffiliate links · may earn us a commission