Skip to content

Constructor

A constructor is a special method that is automatically called when an object is created. Its primary purpose is to initialize the new object — set up initial values, allocate resources, and perform any setup the object needs before it’s used.

  • Ensures objects are created in a valid initial state
  • Automates initialization — no manual setup after creation
  • Allows parameterization — creating objects with different initial values
  • To guarantee objects are ready to use immediately after creation
  • To enforce required parameters for object creation
  • To set up default values and configurations

When you build a house, the constructors set it up:

  • Default constructor: You get a basic empty lot
  • Parameterized constructor: You specify the number of rooms, color, and layout
  • Constructor chaining: You start with a foundation, then add floors, then roof

Without a constructor, you’d have a pile of materials. With it, you get a ready-to-move-in house.


A constructor is a special method that:

  • Has the same name as the class (in Java, C#)
  • Is called automatically when using the new keyword
  • Has no return type (not even void)
  • Initializes the object’s state

Without constructors:

// Without a constructor — manual setup, easy to forget
const user = {};
user.name = "John";
user.email = "john@email.com";
// What if we forget to set email? The object is in an invalid state.

With constructors:

class User {
constructor(name, email) {
this.name = name;
this.email = email;
this.isActive = true; // Default value
}
}
// Object is fully initialized after creation
const user = new User("John", "john@email.com");

sequenceDiagram
participant Client
participant Class
participant Object
Client->>Class: new ClassName(args)
Class->>Class: 1. Allocate memory
Class->>Class: 2. Initialize default values
Class->>Object: 3. Call constructor(args)
Object->>Object: 4. Set properties
Object-->>Client: Return object reference
Client->>Object: 5. Use object.method()

A constructor with no parameters. If you don’t define any constructor, most languages provide a default one automatically.

// JavaScript — if no constructor is defined, an empty one is implied
class Car {
// Default constructor is: constructor() {}
}
const car = new Car(); // Works, but properties are undefined
// Explicit default constructor
class Car {
constructor() {
this.brand = "Unknown";
this.color = "White";
}
}
const car = new Car();
console.log(car.brand); // "Unknown"

Accepts arguments to initialize the object with specific values.

class Car {
constructor(brand, color, year) {
this.brand = brand;
this.color = color;
this.year = year;
}
}
const car = new Car("Toyota", "Red", 2023);
console.log(car.brand); // "Toyota"

3. Constructor Chaining (Calling parent constructor)

Section titled “3. Constructor Chaining (Calling parent constructor)”

When a class inherits from another, the child constructor must call the parent constructor.

class Vehicle {
constructor(type) {
this.type = type;
}
}
class Car extends Vehicle {
constructor(brand, color) {
super("Car"); // Call parent constructor FIRST
this.brand = brand;
this.color = color;
}
}

class Book {
// Default values with parameterized constructor
constructor(title = "Untitled", author = "Unknown", pages = 0) {
this.title = title;
this.author = author;
this.pages = pages;
this.isCheckedOut = false;
this.createdAt = new Date();
}
getInfo() {
return `'${this.title}' by ${this.author}`;
}
}
const book1 = new Book("1984", "George Orwell", 328);
const book2 = new Book(); // Uses defaults
const book3 = new Book("The Hobbit"); // Partial parameters
console.log(book1.getInfo()); // "'1984' by George Orwell"
console.log(book2.title); // "Untitled"
console.log(book3.title); // "The Hobbit"
console.log(book3.author); // "Unknown"

Some languages support multiple constructors with different parameter lists.

public class Message {
private String text;
private String sender;
private long timestamp;
public Message() {
this("", "System");
}
public Message(String text) {
this(text, "Anonymous");
}
public Message(String text, String sender) {
this.text = text;
this.sender = sender;
this.timestamp = System.currentTimeMillis();
}
}

Note: JavaScript and Python don’t support constructor overloading. Use default parameters or factory methods instead.


Factory Method Pattern (Alternative to overloading)

Section titled “Factory Method Pattern (Alternative to overloading)”
class Logger {
constructor(level) {
this.level = level;
}
// Static factory methods
static createDevLogger() {
return new Logger("debug");
}
static createProdLogger() {
return new Logger("error");
}
static createTestLogger() {
return new Logger("silent");
}
}
const logger = Logger.createDevLogger();

Builder Pattern (For complex construction)

Section titled “Builder Pattern (For complex construction)”
class Pizza {
constructor() {
this.size = "medium";
this.toppings = [];
this.crust = "regular";
}
}
class PizzaBuilder {
constructor() { this.pizza = new Pizza(); }
setSize(size) { this.pizza.size = size; return this; }
addTopping(topping) { this.pizza.toppings.push(topping); return this; }
setCrust(crust) { this.pizza.crust = crust; return this; }
build() { return this.pizza; }
}
const pizza = new PizzaBuilder()
.setSize("large")
.addTopping("pepperoni")
.addTopping("mushrooms")
.setCrust("stuffed")
.build();

  • Validate parameters in the constructor to ensure valid state
  • Don’t do heavy work in constructors — initialize only, defer heavy logic
  • Use default parameters for optional values
  • Keep constructors simple — if it’s complex, use a factory or builder
  • Call super() first in derived class constructors
  • Avoid throwing exceptions in constructors when possible

  • Calling methods that can be overridden in constructors (Java/C#)
  • Performing I/O in constructors — makes testing difficult
  • Leaking this reference before object is fully constructed
  • Too many parameters — consider an options object or builder
  • Not calling super() in derived class constructors

Easy:

  1. What is a constructor?
  2. What’s the difference between a default and parameterized constructor?
  3. Is a constructor required in a class?

Medium: 4. What is constructor chaining and why is it needed? 5. Can constructors be inherited? 6. What is constructor overloading?

Advanced: 7. How does JavaScript’s constructor differ from Java’s constructor? 8. What is the difference between __init__ and __new__ in Python? 9. Explain the builder pattern as an alternative to complex constructors.


ConceptKey Point
ConstructorSpecial method called when creating an object
PurposeInitialize object to a valid state
DefaultNo-args constructor provided automatically if none defined
ParameterizedConstructor with parameters for custom initialization
Chainingsuper() calls parent constructor in inheritance
OverloadingMultiple constructors with different parameters (Java/C#)
FactoryAlternative pattern for flexible object creation

Previous Topic: Object → Next Topic: this Keyword → Related Topics: Class, Object Lifecycle