0% found this document useful (0 votes)
2 views

lec 3_constructor

Java constructors are special methods used to initialize objects when they are created, with rules including having the same name as the class and not being abstract or static. There are different types of constructors: default, parameterized, and copy constructors, each serving specific purposes in object creation and initialization. Constructor overloading allows multiple constructors with different parameters to exist within a class, enhancing flexibility in object instantiation.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

lec 3_constructor

Java constructors are special methods used to initialize objects when they are created, with rules including having the same name as the class and not being abstract or static. There are different types of constructors: default, parameterized, and copy constructors, each serving specific purposes in object creation and initialization. Constructor overloading allows multiple constructors with different parameters to exist within a class, enhancing flexibility in object instantiation.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Java Constructors

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.

 Rules for writing constructors are as follows:


 The constructor(s) of a class must have the same name as the class name in which it resides.
 A constructor in Java can not be abstract, final, static, or Synchronized.
 Access modifiers can be used in constructor declaration to control its access i.e which other class
can call the constructor.

Java Constructors are Different From Java Methods?

 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();

Imp. Types of Constructors in Java

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.

// Java Program to demonstrate


// Default Constructor
import java.io.*;

// Driver class
class GFG {

// Default Constructor
GFG() { System.out.println("Default constructor"); }

// Driver function
public static void main(String[] args)
{
GFG hello = new GFG();
}
}

2.Parameterized Constructor: A constructor that has parameters is known as parameterized constructor.


If we want to initialize fields of the class with our own values, then use a parameterized constructor.
/ Java Program for Parameterized Constructor
import java.io.*;
class Geek {
// data members of the class.
String name;
int id;
Geek(String name, int id)
{
this.name = name;
this.id = id;
}
}
class GFG {
public static void main(String[] args)
{
// This would invoke the parameterized constructor.
Geek geek1 = new Geek("Avinash", 68);
System.out.println("GeekName :" + geek1.name
+ " and GeekId :" + geek1.id);
}
}

(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);
}

// constructor with two arguments


Geek(String name, int age)
{

System.out.println(
"Constructor with two arguments : "
+ " String and Integer : " + name + " " + age);
}

// Constructor with one argument but with different


// type than previous..
Geek(long id)
{
System.out.println(
"Constructor with one argument : "
+ "Long : " + id);
}
}

class GFG {
public static void main(String[] args)
{
// Creating the objects of the class named 'Geek'
// by passing different arguments

// Invoke the constructor with one argument of


// type 'String'.
Geek geek2 = new Geek("Shikhar");

// Invoke the constructor with two arguments


Geek geek3 = new Geek("Dharmesh", 26);
// Invoke the constructor with one argument of
// type 'Long'.
Geek geek4 = new Geek(325614567);
}
}

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.

// Java Program for Copy Constructor


import java.io.*;

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();

// This would invoke the copy constructor.


Geek geek2 = new Geek(geek1);
System.out.println(
"Copy Constructor used Second Object");
System.out.println("GeekName :" + geek2.name
+ " and GeekId :" + geek2.id);
}
}

You might also like