Understanding OOP Concepts: Constructor & Destructor

In this topic, we’re going to take the concept of Constructor & Destructor. I’ll write the main concept only.

Table of Contents

  1. Constructor
  2. Constructor Overloading
  3. Destructor
  4. Difference Between Constructor & Destructor

Constructor

A constructor is a special method that is used to initialize the object’s state. The constructor is called automatically during object creation. It’s not mandatory to write a constructor at the time of creating a class.

Let’s see an example in Java:

public class Test {

    int a;
    int b;

    Test() {
        a = 10;
        b = 20;
        System.out.println("I'm a Constructor");
    }

    public void display() {
        System.out.println("Value of a: " + a);
        System.out.println("Value of b: " + b);
    }

    public static void main(String[] arg) {
        // object
        Test test = new Test();
        test.display();
    }
}

Constructor Overloading

The constructor can be overloaded. Constructor overloading is a concept of having more than one constructor with different parameters.

An example may help us to understand more:

public class Test {

    int a;
    int b;

    Test() {
        a = 10;
        b = 20;
        System.out.println("I'm the first Constructor");
    }

    Test(int value) {
        a = value;
        b = 40;
        System.out.println("I'm the second Constructor");
    }

    public void display() {
        System.out.println("Value of a: " + a);
        System.out.println("Value of b: " + b + "\n");
    }

    public static void main(String[] arg) {
        // object 1
        Test test1 = new Test();
        test1.display();

        // object 2
        Test test2 = new Test(30);
        test2.display();
    }
}

Destructor

A destructor is a special method and it’s called when an object’s lifecycle is over to free up the memory.

Destructor in Java: We don’t need a destructor because Java has an excellent garbage collection mechanism to free up the memory. The Garbage Collector (a program of Java & run on JVM) automatically deletes the unused objects.

Still, if you need to use destructor in Java, you can use object.finalize() method. It works like a destructor.

Difference Between Constructor & Destructor

Let’s see the difference between constructor & destructor:

ConstructorDestructor
A constructor is used to initialize objects of a class.Destructor destroys the objects when they are no longer needed.
A constructor is called when object is created.Destructor is called when instance of a class is deleted or released.
A constructor allocates memory.Destructor releases the memory.
Overloading is possibleOverloading is not allowed
A constructor cannot be declared virtual.A destructor can be virtual
Constructor accepts arguments.Destructor does not accept any argument.

Software Engineer | Ethical Hacker & Cybersecurity...

Md Obydullah is a software engineer and full stack developer specialist at Laravel, Django, Vue.js, Node.js, Android, Linux Server, and Ethichal Hacking.