Understanding OOP Concepts: Constructor & Destructor

Last modified on February 22, 2020 1 min read

In this topic, we’re going to take the concepts of Constructor & Destructor. I’ll write the main concepts 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:

Constructor Destructor
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 possible Overloading is not allowed
A constructor cannot be declared virtual. A destructor can be virtual
Constructor accepts arguments. Destructor does not accept any argument.

Author

Hey, I'm Md Obydullah. I build open-source projects and write about Laravel, Linux server, modern JavaScript and more on web development. If you enjoy my content, please consider supporting what I do!

Follow Buy me a coffeeBuy me a coffee

Leave a Reply

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