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
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. |
Comment
Preview may take a few seconds to load.
Markdown Basics
Below you will find some common used markdown syntax. For a deeper dive in Markdown check out this Cheat Sheet
Bold & Italic
Italics *asterisks*
Bold **double asterisks**
Code
Inline Code
`backtick`Code Block```
Three back ticks and then enter your code blocks here.
```
Headers
# This is a Heading 1
## This is a Heading 2
### This is a Heading 3
Quotes
> type a greater than sign and start typing your quote.
Links
You can add links by adding text inside of [] and the link inside of (), like so:
Lists
To add a numbered list you can simply start with a number and a ., like so:
1. The first item in my list
For an unordered list, you can add a dash -, like so:
- The start of my list
Images
You can add images by selecting the image icon, which will upload and add an image to the editor, or you can manually add the image by adding an exclamation !, followed by the alt text inside of [], and the image URL inside of (), like so:
Dividers
To add a divider you can add three dashes or three asterisks:
--- or ***

Comments (0)