Tutorials Home   >   Object-Oriented Programming (OOP)   >   Difference Between Constructor and Destructor

Difference Between Constructor and Destructor

In Object-Oriented Programming (OOP), when we create and use objects, two special methods are very important: constructor and destructor.

  • A constructor sets up an object when it is created.

  • A destructor cleans up the object when it is no longer needed.

Understanding the difference between constructor and destructor is essential for writing efficient and error-free programs.

In this explanation, you will learn:

  • What constructors are

  • What destructors are

  • How they work

  • Key differences between them

  • Examples and common mistakes


1. What Is a Constructor?

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

Key Points:

  • Initializes object attributes

  • May be default, parameterized, or copy constructor

  • Runs once when object is created

Example in Python

class Car:
def __init__(self, color):
self.color = color
print("Car created with color", self.color)
myCar = Car(“Red”) # Output: Car created with color Red

Here:

  • __init__ is the constructor

  • Called automatically when myCar is created


2. What Is a Destructor?

A destructor is a special method that is automatically called when an object is destroyed.

Key Points:

  • Cleans up resources used by the object (memory, files, etc.)

  • Runs once when the object goes out of scope or is deleted

  • In Python, the destructor is __del__()

Example in Python

class Car:
def __init__(self, color):
self.color = color
print("Car created with color", self.color)
def __del__(self):
print(“Car destroyed with color”, self.color)

myCar = Car(“Red”)
del myCar # Output: Car destroyed with color Red

Here:

  • __del__ is the destructor

  • Called automatically when myCar is deleted


3. Key Differences Between Constructor and Destructor

Feature Constructor Destructor
Purpose Initialize object Destroy object / clean up resources
Called Automatically when object is created Automatically when object is destroyed or deleted
Parameters Can have parameters (parameterized constructor) Usually has no parameters
Return Type None None
Frequency Runs once per object creation Runs once per object destruction
Name __init__ (Python), same as class (C++, Java) __del__ (Python), same as class with ~ in C++
Execution Time At the start of object creation At the end of object lifetime
Usage Set initial values Release memory, close files, free resources
Example Car("Red") → sets color del Car → cleans up

4. How Constructor and Destructor Work Together

  1. Object is created → constructor runs

  2. Program uses object → methods and attributes are accessed

  3. Object is deleted or goes out of scope → destructor runs

Example in Python

class Lamp:
def __init__(self, color):
self.color = color
print("Lamp created:", self.color)
def __del__(self):
print(“Lamp destroyed:”, self.color)

lamp1 = Lamp(“Blue”)
lamp2 = Lamp(“Green”)
del lamp1 # Lamp destroyed: Blue
# lamp2 will be destroyed automatically when program ends


5. Advantages of Constructor

  1. Automatic Initialization – Object is ready to use

  2. Custom Values – Parameterized constructor allows different initial values

  3. Reduces Errors – Ensures required attributes are set

  4. Cleaner Code – No need to call a separate setup method


6. Advantages of Destructor

  1. Automatic Cleanup – Frees memory and resources

  2. Prevents Memory Leaks – Especially in languages like C++

  3. Simplifies Resource Management – Closes files, network connections automatically

  4. Maintains Program Stability – Objects are properly destroyed


7. Common Mistakes Beginners Make

  • Forgetting to use self in Python constructors

  • Calling destructor manually unnecessarily

  • Confusing when constructor vs destructor runs

  • Not releasing resources in destructor in languages like C++

  • Overloading destructor (not allowed in most languages)


8. Real-World Analogy

Think of a constructor and destructor like a hotel room:

  • Constructor: Prepares the room for the guest – makes the bed, sets towels, provides keys

  • Destructor: Cleans the room after the guest leaves – removes trash, changes sheets, frees the room

Another example:

  • Constructor → assembling a new toy when purchased

  • Destructor → putting the toy away or recycling when done


9. Conclusion

  • Constructor → sets up the object at creation

  • Destructor → cleans up the object at destruction

Together, constructors and destructors help programmers manage objects efficiently, ensuring proper initialization and cleanup.

By understanding this difference, beginners can write reliable and resource-friendly programs.

As you continue learning OOP, remember:

Constructor = setup, Destructor = cleanup.