lec 3_constructor
lec 3_constructor
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 the constructor, memory for the object is allocated in the memory. It is a
special type of method that is used to initialize the object. Every time an object is created using the
new() keyword, at least one constructor is called. constructors are used to assign values to the class
variables at the time of object creation.
Constructors must have the same name as the class within which it is defined it is not necessary
for the method in Java.
Constructors do not return any type while method(s) have the return type or void if does not
return any value.
Constructors are called only once at the time of Object creation while method(s) can be called
any number of times.
class Geek
{
.......
// A Constructor
Geek() {
}
.......
}
// We can create an object of the above class
// using the below statement. This statement
// calls above constructor.
Geek obj = new Geek();
1.Default Constructor: A constructor that has no parameters is known as default the constructor. A
default constructor is invisible. And if we write a constructor with no arguments, the compiler does not
create a default constructor. It is taken out. It is being overloaded and called a parameterized
constructor. The default constructor changed into the parameterized constructor. But Parameterized
constructor can’t change the default constructor. The default constructor can be implicit or explicit. If we
don’t define explicitly, we get an implicit default constructor. If we manually write a constructor, the
implicit one is overridded.
// Driver class
class GFG {
// Default Constructor
GFG() { System.out.println("Default constructor"); }
// Driver function
public static void main(String[] args)
{
GFG hello = new GFG();
}
}
(imp)Now the most important topic that comes into play is the strong incorporation of OOPS with
constructors known as constructor overloading. Just like methods, we can overload constructors for
creating objects in different ways. The compiler differentiates constructors on the basis of the number of
parameters, types of parameters, and order of the parameters.
/ Java Program to illustrate constructor overloading
// using same task (addition operation ) for different
// types of arguments.
import java.io.*;
class Geek {
// constructor with one argument
Geek(String name)
{
System.out.println("Constructor with one "
+ "argument - String : " + name);
}
System.out.println(
"Constructor with two arguments : "
+ " String and Integer : " + name + " " + age);
}
class GFG {
public static void main(String[] args)
{
// Creating the objects of the class named 'Geek'
// by passing different arguments
3.Copy Constructor: Unlike other constructors copy constructor is passed with another object which
copies the data available from the passed object to the newly created object.
class Geek {
// data members of the class.
String name;
int id;
// Parameterized Constructor
Geek(String name, int id)
{
this.name = name;
this.id = id;
}
// Copy Constructor
Geek(Geek obj2)
{
this.name = obj2.name;
this.id = obj2.id;
}
}
class GFG {
public static void main(String[] args)
{
// This would invoke the parameterized constructor.
System.out.println("First Object");
Geek geek1 = new Geek("Avinash", 68);
System.out.println("GeekName :" + geek1.name
+ " and GeekId :" + geek1.id);
System.out.println();