Unit 3 Notes
Unit 3 Notes
Constructors in Java
In Java, a constructor is a block of codes similar to the method. It is called when an
instance of the class is created. At the time of calling constructor, memory for the object
is allocated in the memory.
Every time an object is created using the new() keyword, at least one constructor is called.
It calls a default constructor if there is no constructor available in the class. In such case,
Java compiler provides a default constructor by default.
There are two types of constructors in Java: no-arg constructor, and parameterized
constructor.
Note: It is called constructor because it constructs the values at the time of object creation.
It is not necessary to write a constructor for a class. It is because java compiler creates
a default constructor if your class doesn't have any.
2. Parameterized constructor
Java Default Constructor
A constructor is called "Default Constructor" when it doesn't have any parameter.
class Bike1{
Bike1(){System.out.println("Bike is created");}
//main method
Output:
Bike is created
int id;
String name;
//creating objects
s1.display();
s2.display();
Output:
0 null
0 null
Explanation:In the above class,you are not creating any constructor so compiler
provides you a default constructor. Here 0 and null values are provided by default
constructor.
In this example, we have created the constructor of Student class that have two
parameters. We can have any number of parameters in the constructor.
class Student4{
int id;
String name;
id = i;
name = n; }
void display()
System.out.println(id+" "+name);
s1.display();
s2.display();
}
}
Output:
111 Karan
222 Aryan
Constructor Overloading in Java
In Java, a constructor is just like a method but without return type. It can also be
overloaded like Java methods.
Constructor overloading in Java is a technique of having more than one constructor with
different parameter lists. They are arranged in a way that each constructor performs a
different task. They are differentiated by the compiler by the number of parameters in the
list and their types.
class Student5{
int id;
String name;
int age;
id = i;
name = n;
}
//creating three arg constructor
Java Copy Constructor
There is no copy constructor in Java. However, we can copy the values from one object
to another like copy constructor in C++.
There are many ways to copy the values of one object into another in Java. They are:
o By constructor
In this example, we are going to copy the values of one object into another using Java
constructor.
//Java program to initialize the values from one object to another object.
class Student6{
int id;
String name;
id = i;
name = n;
Student6(Student6 s){
id = s.id;
name =s.name;
s1.display();
s2.display();
}
Output:
111 Karan
111 Karan
Copying values without constructor
We can copy the values of one object into another by assigning the objects values to
another object. In this case, there is no need to create the constructor.
1. class Student7{
2. int id;
3. String name;
4. Student7(int i,String n){
5. id = i;
6. name = n;
7. }
8. Student7(){}
9. void display(){System.out.println(id+"
"+name);} 10.
Output:
111 Karan
111 Karan
Q) Does constructor return any value?
Yes, it is the current class instance (You cannot use return type yet it returns a value).
Yes, like object creation, starting a thread, calling a method, etc. You can perform any
operation in the constructor as you perform in the method.
Yes.
Java provides a Constructor class which can be used to get the internal information of a
constructor in the class. It is found in the java.lang.reflect package.
id = i;
name = n;
age=a;
Student5 s2 = new
Student5(222,"Aryan",25); s1.display();
s2.display();
}
Output:
111 Karan 0
222 Aryan 25
Difference between constructor and method in Java
There are many differences between constructors and methods. They are given
below.
The Java compiler provides The method is not provided by the compiler in any
a default constructor if you case.
don't have any constructor
in a class.
The constructor name must The method name may or may not be same as the
be same as the class name. class name.
There is no copy constructor in Java. However, we can copy the values from one object
to another like copy constructor in C++.
There are many ways to copy the values of one object into another in Java. They are:
o By constructor
In this example, we are going to copy the values of one object into another using Java
constructor.
//Java program to initialize the values from one object to another object.
class Student6{
int id;
String name;
id = i;
name = n;
}
//constructor to initialize another object
Student6(Student6 s){
id = s.id;
name =s.name;
s1.display();
s2.display();
}
}
Output:
111 Karan
111 Karan
We can copy the values of one object into another by assigning the objects values to
another object. In this case, there is no need to create the constructor.
class Student7{
int id;
String name;
id = i;
name = n;
Student7(){}
s2.id=s1.id;
s2.name=s1.name;
s1.display();
s2.display();
Output:
111 Karan
111 Karan
Yes, it is the current class instance (You cannot use return type yet it returns
a value).
Yes, like object creation, starting a thread, calling a method, etc. You can
perform any operation in the constructor as you perform in the method.
Yes.
What is a destructor?
Destructor is an instance member function that is invoked automatically whenever an object
is going to be destroyed. Meaning, a destructor is the last function that is going to be called
before an object is destroyed.
A destructor is also a special member function like a constructor. Destructor destroys
the class objects created by the constructor.
Destructor has the same name as their class name preceded by a tilde (~) symbol.
It is not possible to define more than one destructor.
The destructor is only one way to destroy the object created by the constructor. Hence
destructor can-not be overloaded.
Destructor neither requires any argument nor returns any value.
It is automatically called when an object goes out of scope.
Destructor release memory space occupied by the objects created by the constructor.
In destructor, objects are destroyed in the reverse of an object creation.
The thing is to be noted here if the object is created by using new or the constructor uses
new to allocate memory that resides in the heap memory or the free store, the destructor
should use delete to free the memory.
Syntax
The syntax for defining the destructor within the class:
~ <class-name>() {
// some instructions
}
The syntax for defining the destructor outside the class:
<class-name> :: ~<class-name>() {
// some instructions
}
Example 1
The below code demonstrates the automatic execution of constructors and destructors when
objects are created and destroyed, respectively.
C++
// and destructor
#include <iostream>
class Test {
public:
// User-Defined Constructor
// User-Defined Destructor
};
main()
{
Test t;
return 0;
Output
Constructor executed
Destructor executed
Properties of Destructor
A destructor function is called automatically when the object goes out of scope:
1. the function ends
2. the program ends
3. a block containing local variables ends
4. a delete operator is called
Note: destructor can also be called explicitly for an object.
Destructors have the same name as the class preceded by a tilde (~)
Destructors don’t take any argument and don’t return anything
C++
#include <bits/stdc++.h>
class String {
private:
char* s;
int size;
public:
String(char*); // constructor
~String(); // destructor
};
String::String(char* c)
size = strlen(c);
strcpy(s, c);
String::~String() { delete[] s; }
int main()
String myString(str);
return 0;
}
Difference between Constructor and Destructor in C++
1. The constructor initializes the class and allots If the object is no longer required, then destructors
the memory to an object. demolish the objects.
2. When the object is created, a constructor is When the program gets terminated, the destructor
called automatically. is called automatically.
4. A constructor allows an object to initialize some A destructor allows an object to execute some code
of its value before it is used. at the time of its destruction.
6. When it comes to constructors, there can be When it comes to destructors, there is constantly a
various constructors in a class. single destructor in the class.
7. They are often called in successive order. They are often called in reverse order of
constructor.