JAVA - Zero to Hero / Object Oriented Programming (OOP)

Constructor

❔ What is a Constructor?

A constructor is a special method that’s automatically called when an object is created.

  • Its purpose is to initialize objects.

  • It has the same name as the class and no return type (not even void).

🏗️ Why Constructors Are Required in Java

🔹 Used for Object Initialization

Constructors are called automatically when an object is created to set initial values.

🔹 Same Name as Class

A constructor must have the same name as the class and has no return type.

🔹 Avoid Manual Setup

Without constructors, you would need to manually set fields using setter methods — constructors make this process easier.

🔹 Allows Overloading

You can create multiple constructors with different parameters to initialize objects in different ways (constructor overloading).

🔹 Ensures Valid State

Constructors can include logic to validate inputs, ensuring the object is always in a valid state when created.

🔹 Improves Readability

Object creation becomes more compact and readable when values are set directly through constructors.

🔹 Default Constructor

If you don't write any constructor, Java provides a default constructor with no parameters.

🔹 Supports Encapsulation

Helps in keeping fields private and still initializing them safely through constructor parameters.

🔹 Used in Inheritance

When a subclass object is created, parent class constructors are called first, helping with proper initialization.

🔸 Syntax:

class ClassName {
    ClassName() {
        // constructor code
    }
}

🛠️ Types of Constructors

Type

Description

Default Constructor

No parameters — used for default setup

Parameterized Constructor

Accepts arguments to initialize values

🏗️ Default Constructor

If you don’t define any constructor, Java automatically creates a default constructor for you.

class Student {
    Student() {
        System.out.println("Default constructor called.");
    }
}

public class Main {
    public static void main(String[] args) {
        Student s1 = new Student();  // constructor auto-called
    }
}
Default constructor called.

➕ Parameterized Constructor

Allows values to be passed when creating an object.

class Student {
    String name;

    Student(String studentName) {
        name = studentName;
    }

    void display() {
        System.out.println("Name: " + name);
    }
}

public class Main {
    public static void main(String[] args) {
        Student s1 = new Student("Aanya");
        s1.display();
    }
}
Name: Aanya

Constructor vs Method

Feature

Constructor

Method

Name

Same as class name

Any name

Return type

None (not even void)

Must have return type

Call

Automatically when object is created

Manually using methodName()

Purpose

Initialize object

Define behavior/operations


🧱 Constructor Overloading

Like methods, constructors can also be overloaded — same name but different parameters.

class Student {
    String name;
    int age;

    Student() {
        name = "Unknown";
        age = 0;
    }

    Student(String n, int a) {
        name = n;
        age = a;
    }

    void display() {
        System.out.println(name + " - " + age);
    }
}

public class Main {
    public static void main(String[] args) {
        Student s1 = new Student();
        Student s2 = new Student("Riya", 20);
        s1.display();
        s2.display();
    }
}
Unknown - 0
Riya - 20

Recap

Concept

Example

Constructor

Student()

Parameterized Constructor

Student(String name)

Overloaded Constructors

Multiple constructors with different params

Auto call

Happens when you do new Student()