Compare the Difference Between Similar Terms

Difference Between

Home / Technology / IT / Programming / Difference Between Constructor and Destructor

Difference Between Constructor and Destructor

February 12, 2018 Posted by Lithmee

Key Difference – Constructor vs Destructor
 

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.

Difference Between Constructor and Destructor

Figure 01: Constructor and Destructor

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

Related posts:

Difference Between Logical Address and Physical AddressDifference Between Logical Address and Physical Address Difference Between Assembler and Compiler Difference Between Inheritance and Composition Difference Between Parallel and Distributed ComputingDifference Between Parallel and Distributed Computing Difference Between Class and Structure in C#Difference Between Class and Structure in C#

Filed Under: Programming Tagged With: Compare Constructor and Destructor Differences, Constructor, Constructor and Destructor Similarities, Constructor Definition, Constructor Invoking, Constructor Number, Constructor Overloading, Constructor Parameters, Constructor Speed, Constructor vs Destructor, Destructor, Destructor Definition, Destructor Invoking, Destructor Number, Destructor Overloading, Destructor Parameters, Destructor Speed

About the Author: Lithmee

Lithmee Mandula is a BEng (Hons) graduate in Computer Systems Engineering. She is currently pursuing a Master’s Degree in Computer Science. Her areas of interests in writing and research include programming, data science, and computer systems.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Request Article

Featured Posts

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and SARS

Difference Between Coronavirus and SARS

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Covid 19

Difference Between Coronavirus and Covid 19

You May Like

Difference Between Marriage License and Marriage Certificate

Difference Between Arabian and Quarter Horses

Difference Between Truvia and Stevia

Difference Between Wii and Wii U

Difference Between Correlational and Experimental Research

Latest Posts

  • What is the Difference Between Declarative and Procedural Memory
  • What is the Difference Between Pancytopenia and Thrombocytopenia
  • What is the Difference Between Sandwich Elisa and Competitive Elisa
  • What is the Difference Between Mayor and Governor
  • What is the Difference Between ABI and TBI
  • What is the Difference Between Primary and Secondary Addison’s Disease
  • Home
  • Vacancies
  • About
  • Request Article
  • Contact Us

Copyright © 2010-2018 Difference Between. All rights reserved. Terms of Use and Privacy Policy: Legal.