Most programming languages support Object Oriented Programming(OOP). It is the paradigm that helps to model a software or a program using objects. OOP improves productivity and maintainability. In OOP everything is considered as an object. The objects are created or instantiated using classes. Constructor and Destructor are common terms in Object Oriented Programming. This article discusses the difference between a constructor and a destructor. A constructor and a destructor are special member function in a class. A constructor and destructor have the same name as the class, but the destructor has a tilde (~) sign. The key difference between a constructor and destructor is that a constructor is used to allocate memory to an object while a destructor is used to the deallocate memory of an object.
CONTENTS
1. Overview and Key Difference
2. What is a Constructor
3. What is a Destructor
4. Similarities Between Constructor and Destructor
5. Side by Side Comparison – Constructor vs Destructor in Tabular Form
6. Summary
What is a Constructor?
A constructor is a special member function in the class to allocate memory to an object. It can be used to provide values for the data members. The constructor is invoked when the object is created. It has the same name as the class name. A constructor does not return any value. Therefore, it does not contain a return type. A constructor can also accept parameters. A constructor with parameters is known as a parameterized constructor.
An example of a constructor is as follows.
public class Rectangle{
int length, width;
public Rectangle(int p, int q){
length = p;
width = q;
}
public int calculateArea(){
return (length * width);
}
}
According to the above piece of code, the constructor has the same name as the class name. The constructor Rectangle accepts two parameters. They are p and q. The integer value p is assigned to the length. The integer value q is assigned to the width. In the calcualteArea, the multiplication of length and width is calculated to find the area of the rectangle. In the main program, the programmer can create an object of type Rectangle and pass the arguments. e.g. Rectangle rect1= new Rectangle( 2,3). Then, the parameterized constructor is called and assigns the values to the length and width.
When there is a constructor without any parameters, it is called a default constructor. If the programmer does not define a constructor, the default constructor will be invoked. If there is a class as Student and when the programmer creates an object of type Student, the default constructor is called. e.g. Student s1= new Student(); There can be multiple constructors with different parameters and different data types in a class. The appropriate constructor can be called accordingly. Therefore, constructors can be overloaded.
What is a Destructor?
A destructor is a special member function in the class. It is used to de-allocate memory for an object created by the constructor. The destructor is invoked when the object is destroyed. It carries out the cleanup storage that is no longer required. Like the constructor, the destructor has the same name as the class. It also contains a tilde (~) symbol.
A destructor does not return any value. Unlike a constructor, the destructor does not accept any parameters. So, destructor does not support overloading. Declaring a destructor is considered as a good programming practice because it releases the memory space and that space can be used for storing some other objects. The syntax of destructor is similar to ~className() { }. e.g. ~Rectangle() { }; There can only be a single destructor in a class.
What are the Similarities Between Constructor and Destructor?
- Both constructor and destructor are associated with objects.
- Both constructor and destructor do not return any value.
- Both constructor and destructor are called automatically.
What is the Difference Between Constructor and Destructor?
Constructor vs Destructor |
|
A constructor is a special member in the class that is used to allocate memory to an object. | A destructor is a special member of the class that is used to deallocate memory of an object. |
Method of Invoking | |
A constructor is invoked when the object is created. | A destructor is called when the object is destroyed or deleted. |
Usage | |
A constructor is used to allocated memory for the objects. | A destructor is used to deallocate memory for the objects. |
Parameters | |
A constructor accepts parameters. | A destructor does not accepts parameters. |
Number of Constructors and Destructors | |
There can be multiple constructors with a different number of parameters and different types of parameters. | There can be single destructor in the class. |
Execution Speed | |
A constructor has the same name as the class name. | A destructor has the same name as the class name with a tilde (~) symbol. |
Overloading | |
A constructor can be overloaded. | A destructor cannot be overloaded. |
Summary – Constructor vs Destructor
OOP is a common paradigm in software development .It can simplify a complex project. A constructor and a destructor are used in OOP. A constructor and destructor have the same name as the class, but the destructor has a ~ sign. The difference between a constructor and destructor is that a constructor is used to allocate memory to an object while a destructor is used to the deallocate memory of an object.
Download the PDF of Constructor vs Destructor
You can download the PDF version of this article and use it for offline purposes as per citation note. Please download the PDF version here: Difference Between Constructor and Destructor
Reference:
1.tutorialspoint.com. “Java Object and Classes.” The Point, Available here