Tutorials Home   >   Object-Oriented Programming (OOP)   >   What is Constructor?

What is Constructor?

In Object-Oriented Programming (OOP), when we create objects from a class, we often want to initialize their data automatically. This is where constructors come in.

A constructor is a special method in a class that runs automatically when an object is created. It sets initial values for the object’s attributes and prepares the object for use.

In this explanation, you will learn:

  • What a constructor is

  • How constructors work

  • Types of constructors

  • Examples in programming

  • Advantages and common mistakes


1. Definition of Constructor

A constructor is a special method in a class that is called automatically when an object is created.

Simple Definition:

A constructor is used to initialize an object’s attributes when it is created.

Key points about constructors:

  • The name is usually the same as the class (Java, C++) or __init__ in Python

  • It does not return a value (not even void in some languages)

  • It is automatically called when creating an object


2. Why Constructors Are Important

  1. Automatic Initialization – No need to call a separate method

  2. Saves Time – Object is ready immediately after creation

  3. Avoids Errors – Ensures required attributes have initial values

  4. Supports OOP Principles – Works with encapsulation and abstraction

  5. Makes Code Cleaner – Reduces repetitive initialization code


3. Types of Constructors

3.1 Default Constructor

  • A constructor with no parameters

  • Initializes attributes with default values

Example in Python

class Car:
def __init__(self):
self.color = "Red"
self.model = "Sedan"
myCar = Car()
print(myCar.color) # Output: Red
print(myCar.model) # Output: Sedan

Here:

  • __init__ is the default constructor

  • Sets color and model automatically


3.2 Parameterized Constructor

  • A constructor that accepts parameters

  • Allows initializing objects with custom values

Example in Python

class Student:
def __init__(self, name, age):
self.name = name
self.age = age
student1 = Student(“Ali”, 16)
student2 = Student(“Sara”, 17)

print(student1.name, student1.age) # Ali 16
print(student2.name, student2.age) # Sara 17

Here:

  • __init__(self, name, age) is parameterized

  • Each object can have different initial values


3.3 Copy Constructor (in some languages like C++/Java)

  • Creates a new object as a copy of an existing object

  • Not explicitly available in Python (can be simulated)

Example in C++

class Student {
public:
string name;
int age;
// Parameterized constructor
Student(string n, int a) {
name = n;
age = a;
}

// Copy constructor
Student(Student &s) {
name = s.name;
age = s.age;
}
};

Student s1(“Ali”, 16);
Student s2(s1); // Copy of s1


4. How Constructors Work

  1. When an object is created, the constructor is automatically called

  2. The constructor initializes attributes

  3. After the constructor finishes, the object is ready to use

Example Workflow

class Lamp:
def __init__(self, color):
self.color = color
print("Lamp created!")
lamp1 = Lamp(“Blue”) # Output: Lamp created!

  • Constructor sets color = "Blue" automatically

  • Prints message during object creation


5. Constructor vs Regular Method

Feature Constructor Regular Method
Called Automatically Yes No
Purpose Initialize object Perform action
Return Type None Can return values
Name Same as class (__init__ in Python) Any name
Example __init__(self, ...) def turn_on(self): ...

6. Advantages of Constructors

  1. Automatic Initialization – Objects are ready immediately

  2. Custom Initialization – Parameterized constructors set values

  3. Reduces Errors – Ensures attributes are initialized properly

  4. Clean Code – No need to call separate setup methods

  5. Supports OOP – Works seamlessly with inheritance and polymorphism


7. Common Mistakes Beginners Make

  1. Forgetting to include self in Python constructors

  2. Using a return statement in a constructor (Python ignores it)

  3. Overloading constructors incorrectly in languages that don’t support it

  4. Forgetting to pass required parameters for a parameterized constructor

  5. Confusing constructors with regular methods


8. Real-World Analogy

Think of a constructor like assembling a new toy or gadget:

  • When you buy a toy, it comes pre-assembled or ready to use

  • You don’t need to manually attach every part

  • If it’s a custom toy, the constructor sets it up according to your preferences

Another analogy:

  • A lamp is created with color, type, and switch in place

  • Once created, it’s ready to use without extra setup


9. Conclusion

A constructor is a special method used to initialize objects automatically.

Key points to remember:

  • Default constructor → no parameters, sets default values

  • Parameterized constructor → sets custom values

  • Copy constructor → copies an existing object

By understanding constructors, beginners can create objects efficiently, reduce errors, and write cleaner programs.