Interview Q&A · Java · 5 questions in this topic

Constructors: Rules, Types & Chaining — Java Interview Q&A

All Java topics

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

  1. 1this(...) picks the overload whose parameter list matches the arguments.
  2. 2It must be the first statement; a second this(...) or super(...) in the same constructor is a compile error.
  3. 3If you write no explicit call, the compiler inserts super() (the no-arg superclass constructor) as the first statement.

Example

Example.javajava
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

  1. 1Default constructor: compiler-provided, no-arg, does nothing extra. Disappears the moment you declare any constructor yourself.
  2. 2Parameterised constructor: takes arguments so callers can set initial state.
  3. 3Constructors are not methods: they are never inherited and never overridden.

Example

Example.javajava
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

  1. 1class Car {} → the compiler adds Car() automatically.
  2. 2class Car { Car(String m) {...} } → new Car() no longer compiles.
  3. 3A common interview bug: adding a parameterised constructor breaks framework or subclass code that relied on the invisible default.

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits