Constructor destructor.ppt

KarthikSekar 113 views 19 slides Sep 24, 2020
Slide 1
Slide 1 of 19
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19

About This Presentation

A constructor is nothing but automatic initialization of the object. Destructor is used to free that memory allocated while initialization.


Slide Content

Slides by S. Karthik Assistant Professor of IT PSG College of Arts & Science Coimbatore – 14.

What is Constructor and Destructor in Java? A constructor is used to initialize a variable that means it allocates memory for the same A constructor is nothing but automatic initialization of the object. Whenever the program creates an object at that time constructor is gets called automatically. Destructor is used to free that memory allocated while initialization. Generally, in java, we don’t need to call the destructor explicitly. Java has a feature of automatic garbage collection.

Why do we Need Constructor and Destructor? Constructor and destructor mostly used to handle memory allocation and de-allocation efficiently. Constructor and destructor do a very important role in any programming language of initializing and destroying it after use to free up the memory space.

How Constructor and Destructor Works in Java A constructor is just a method in java. Which has the same name as the class name. The constructor method does not have any return type to it.

class Employee { Employee() { } } If you see in this example we have not given any return type like int or void to the method which has the same name as a class name. It is mainly used to initialize the object. When we are creating an object of a class at that time constructor get invoked. Example

How to create Constructors and Destructors class Employee { Employee() { //This is constructor. It has same name as class name. System.out.println (“This is the default constructor”); } }  

Types of Constructor There are two types of constructors depending upon the type we can add and remove variables. Default Constructor Parameterized Constructor

1. Default Constructor This is the one type of constructor. By default without any parameters, this constructor takes place. This constructor does not have any parameters in it.   Class Abc { Abc (){ System.out.println (“This is the example of  default constructor.”); } }

2. Parameterized Constructor As the name suggest parameterized constructor has some parameters or arguments at the time of initializing the object. class Square{ int width,height ; Square( int a , int b){ width = a; height = b; } int area(){ return width * height; } } class Cal{ public static void main(String[] args ){ { Square s1 = new Square(10,20); int area_of_sqaure = s1.area(); System.out.println ("The area of square is:" + area_of_sqaure ); } } }

public class Abc { String name; int quantity; int price; Abc ( String n1, int q1, int p1){ name = n1; quantity = q1; price = p1; } Abc ( String n2, int p2){ name = n2; price = p2; quantity = price/10; } void display(){ System.out.println ("Product Name"+ name); System.out.println ("Product quantity is"+ quantity); System.out.println ("Product price is:"+ price); } public static void main(String[] args ){ Abc product1; product1 = new Abc ("Dates",500,50); product1.display(); product1 = new Abc ("cashu",800); product1.display(); } }

Output: Product Name Dates Product quantity is 500 Product price is 50 Product Name cashu Product quantity is 80 Product price is 800

Destructor Destructor is in C++ programming language. If we are talking about java then java has a feature called automatic garbage collector. Which free the dynamically allocated memory when there is no use. This concept is very important and you can explore more about this garbage collection in java.

Java uses the garb collection technique for memory allocation automatically. There is no need to explicit use of destructors like C++. For allocating memory in java we do not have malloc function like in C programming. The same process of allocating memory is done by the new operator in java.

new keyword allocates memory space for an object on heap memory. At the time of program execution, a new keyword allocates some memory space for the object. End-user need to worry about this as memory allocation is handled by the program. At the time when the object used in programs done with the work the memory used for the object is utilized for another task. This process of utilizing memory efficiently is the job of garbage collection in java.

Finalize() Methods Finalize method is work like destructor and opposite of constructor as we have seen earlier. Generally, the finalize method is used to remove the object. For using this method we have to explicitly define this method in java. The finalize method starts working after garbage collection done with its work.

This simply means that after freeing memory space by deallocating memory space from objects there are chances that memory utilization still there with other things like fonts etc. to delete that memory space or to frees up that space we make use of finalize() method.

Conclusion Constructor and destructor(garbage collection in java) are very important things to get clear in any programming language as this is the start where you can actually get how things are done at the background to manage memory space. Reference: https://www.educba.com/constructor-and-destructor-in-java/ www.javatpoint.com › java-constructor

Thank You