Constructors in Java
Definition
A constructor is a special method in Java used to initialize instance
variables.
It is called when an object of a class is created.
Key Characteristics
- Constructor Name: The name
of the constructor must be the same as the class name.
- No Return Type:
Constructors do not have a return type, not even
void.
Types of Constructors in Java
In Java, there are primarily two types of constructors:
- Default Constructor
- Parameterized Constructor
1. Default Constructor:
·
If constructor does not have any arguments or
parameters,it is called Default constructor
·
It has no parameters and initializes the object
with default values.
·
Constructor is called, if we create object for
the class. If we create the object for the class, it calls constructor by
default.
·
Every time default constructor is called, if we create
the object for the class.
·
A default constructor always initializes
instance variables with the same default values. This means that every time an
object is created using the default constructor, the instance variables are set
to their default values as specified by the constructor or the class
definition.
// constr
name and class name must be same
//Syntax
:
// className()
// {
// // Initialise instance var
// }
class employee
{
// define constructor
employee() // constructor
{
}
}
classs A
{
// define constructor
A() // constr
{
}
}
class B
{
// // define constructor
B() // constr
{
}
}
HW define constr for fruit class
HW define constr for Animal class
Ex1:
package OOPSBasics;
public class Student
{
//define
Instance var---> name;address;
String
name, address;
//
define constr for Student -class
Student()////
constr or Default constr (D.C- if
constr does not have any args )
{
System.out.println("const is called when
we create an object for class");
// to initialise instanse variable name =
"Ram"; address = "Bng";
name = "Ram";
address = "Bng";
}
public
static void main(String[] args) {
// we have to create obj for the class -Student-
s1 ref var
// it calls constr by default
Student
s1 = new Student();
//
display s1 name
System.out.println("s1
name="+ s1.name);
//
Display s1 address
System.out.println("s1
address="+ s1.address);
//
create obj for the class -Student- s2 ref var
Student
s2 = new Student();
//
display s2 name
System.out.println("s2
name="+ s2.name);
//
Display s2 address
System.out.println("s2
address="+ s2.address);
}
}
o/p:
const is called when we create an
object for class
s1 name=Ram
s1 address=Bng
const is called when we create an
object for class
s2 name=Ram
s2 address=Bng
2.Parameter constructor:
A parameterized constructor is a
special type of constructor in Java that accepts arguments.
Or
A parameterized constructor is a
constructor that takes one or more parameters.
This allows the initialization of
objects with specific values when they are created.
Syntax:
public ClassName(parameter1, parameter2, ...) {
// constructor body
}
No
Default Constructor: If only parameterized constructors are
defined and no default constructor is present, the compiler will not provide a
default constructor. Therefore, creating an object without arguments will
result in a compilation error.
Examples:
·
Single Parameter Constructor:
Student2(String name) { // Single
parameter constructor
System.out.println("Single Param
constructor is called");
}
Two
Parameter Constructor:
Student2(String s, String a) { //
Two parameter constructor
System.out.println("2 Parameter
constructor is called");
}
Ex: of Paramter constrctor
package OOPSBasics;
public class Student2
{
//
Declare instance variables -name ,
address
String
name,address;
//
Define default constr
//
Constructor ---- Default constr
// if constr
does not have any Parameters -- DC.
Student2()
// Def constr
{
System.out.println("Calling
Default constructor ");
//
// Initiliase instance var name
= "Ram"; address
= "Bangalore";
name=
"Ram";
address
= "Bangalore";
}
//Define
Parameter Constructor : we have to pass some Parameters in
constructor - PC
//
if constr has any Parameters- PC
//
Define 1 PC: pass String param
Student2(String
name)// single Param constr -
{
System.out.println("Single
Parm constr is called ");
}
//
Define 2 Param const- pass String,
String param
Student2(String
s, String a) // 2 param constr
{
System.out.println("2
Parameter constr is called ");
}
public
static void main(String[] args)
{
// if we create obj, constr will be called by
default
// we dont need to call contr explicitly
System.out.println("in
main");
//Create
obj for Student 2 class - s1
Student2
s1 = new Student2();// calls Default constr
//
call 1 Param const
Student2 s2 = new Student2("Raju");
//
call 1 Param Const
Student2 s3 = new Student2("Sita");
//call
2 param const
Student2 s4 = new
Student2("Lucky","Chennai");
//
can we pass int , int value?
// Student2
s4 = new Student2(50, 100);
//
The constructor Student2(int, int) is undefined
}
}
o/p:
in main
Calling Default constructor
Single Parm constr is called
Single Parm constr is called
2 Parameter constr is called
Can we initialise static variable in constructor?
ex:
package OOPSBasics;
public class Student
{
//Declare Instance variable name, address
String
name;
String
address;
//
declare static variable eid
static
int eid; // static var
//
Define Def constr for Student class
Student()
// constr or Default constr (D.C- if
constr does not have any args )
{
System.out.println("const
is called when we create an object for class");
//
to initialise instance var
name = "Ram"; address =
"Bng";, eid =
10;
name
= "Ram";
address
= "Bng";
eid
= 10;
// Constr-
can be used to initialise the static variable also
}
public
static void main(String[] args) {
// we have to create obj for the Student
class with ref variable -s1
Student
s1 = new Student();
// it calls constr by default
//
display s1 name
System.out.println("s1
name="+ s1.name);
//
display s1 address
System.out.println("s1
address="+ s1.address);
//
display s1 eid
System.out.println("s1
eid="+ s1.eid);
// we have to create obj for the Student
class with ref variable -s2
Student
s2 = new Student();
// it calls constr by default
//
display s2 name
System.out.println("s2
name="+ s2.name);
//
display s2 address
System.out.println("s2
address="+ s2.address);
//
display s2 eid
System.out.println("s2
eid="+ s2.eid);
}
}
o/p:
const is called when we create an
object for class
s1 name=Ram
s1 address=Bng
s1 eid=10
const is called when we create an
object for class
s2 name=Ram
s2 address=Bng
s2 eid=10
Write the o/p:
package OOPSBasics;
public class Student3 {
//
instance variables
String name; // null
String
address;// null
// can be used to initialise instance var
Student3()
// def constr
{
System.out.println("Calling
Def constr ");
System.out.println("name="
+ name + ", Address="+ address);
// nul null
//initialise
instance var
name
= "Raju";
address
= "Bangalore";
System.out.println("name="
+ name + ", Address="+ address);
// Raju Bangalore
}
public
static void main(String[] args)
{
System.out.println("in
main");
Student3
s1 = new Student3(); // calls Def constr
// 1. initialise instan var
// s1.name =
"Ramu";
// s1.address =
"Bng";
// System.out.println("s1
name="+ s1.name);// Ramu
Student3
s2 = new Student3(); // // calls Def constr
}
}
o/p:
in main
Calling Def constr
name=null, Address=null
name=Raju, Address=Bangalore
Calling Def constr
name=null, Address=null
name=Raju, Address=Bangalore
·
Param constr- can be used to initialise the
instance variable with diff values
·
DC -- can
be used to initialise the instance variable with same values.
Write o/p for below program?
package OOPSBasics;
public class Student4
{
String
name;
String
address;
//
DEF constr
Student4()
{
System.out.println("DC
is called");
}
Student4(String
sname, String saddress)// 2 Param constr
{
System.out.println("calling
2 Param constr");
name
= sname;
address
= saddress;
System.out.println("name="+
name + ",address="+ address);
}
public
static void main(String[] args)
{
Student4
s1 = new Student4("Ram","Bng");
Student4
s2 = new Student4("Sita","Chennai");
Student4
s3 = new Student4("Raju","Hyd");
}
}
o/p:
calling 2 Param constr
name=Ram,address=Bng
calling 2 Param constr
name=Sita,address=Chennai
calling 2 Param constr
name=Raju,address=Hyd
Constructor Over loading:
FAQ : Can we over load constructor ?
Yes. we can over load constr by
passing diff no of args,
diff
no of data types,
changing
position of data types
// Define default constr
Student()
{
System.out.println("Dc
is called ");
}
//Define constr with 1 arg
Student(String
name)
{
System.out.println("1
Pc is called ");
}
// Define constr with 2 args
Student(String
name, String address)
{
System.out.println("2
Pc is called ");
}
// Student4(String sname, String saddress)//CE: Duplicate method Student4(String, String) in
type Student4
// {
//
// }
ex:Calling DC, 1PC, 2PC
package OOPSBasics;
public class Student7
{
//
instance variables
String name;
String
address;
// can be used to initialise instance var
public
Student7() /// Dc -
no params/args
{
System.out.println("Dc
is called ");
}
public
Student7(String sname) // PC 1 param/
args
{ //Sita. so sname = Sita
System.out.println("1 PC is called ");
}
Student7(String
sname, String sAddress) // 2 PC -- with 2 para
{
// Ramu Bangalore
System.out.println("2
PC is called");
}
public
static void main(String[] args)
{
Student7 s1 = new Student7();
Student7 s2 = new Student7("sita");
Student7 s3 = new Student7("Ramu",
"Bangalore");
//
without defining DC,
}
}
o/p:
Dc is called
1
PC is called
2 PC is called
FAQ : What is constructor? what is main use of constructor?
A constructor
in Java is a special method used to initialize instance variables and static
variables.
DC can be used to initialize the
instance variable with same values.
Param constr can be used to initialize
the instance variable with diff values
Student4 s1 = new
Student4("Ram","Bng");
No comments:
Post a Comment