Constructor
Constructor:-
A constructor is the special member method which will be called automatically (implicitly) whenever an object is creating for placing our own values without placing default values.
The purpose of constructor concept is to initialize an object.
Advantages of constructor: -
If we develop any java application with the concept of constructor then we get the following advantages
1. Constructor eliminates in placing default values.
2. Constructor eliminates in calling ordinary methods explicitly.
A constructor is the special member method which will be called automatically (implicitly) whenever an object is creating for placing our own values without placing default values.
The purpose of constructor concept is to initialize an object.
Advantages of constructor: -
If we develop any java application with the concept of constructor then we get the following advantages
1. Constructor eliminates in placing default values.
2. Constructor eliminates in calling ordinary methods explicitly.
Rules/characteristics/ properties of constructors: -
Why we are using constructors in our java program, it is mandatory to the java program to follow the following rules.
1. Constructor name must be similar to class name.
2. Constructor will be called implicitly whenever an object is created.
3. Constructor should not return any value even void as a return type (if we write
any return type then they acts as ordinary methods).
4. The modifier of the constructor should not be static (because constructors will be
called each and every time whenever an object is created).
5. Constructor never participates in inheritance classes that means constructor of
base class never inherited in to the context of derived class (because every class
constructor is meant of initialing its own data members but not meant for
initializing other class data members).
6. The access specifier of the constructor of a class may be private may not be
private.
a) If the access specifier of a constructor is private then object of a corresponding
class cannot be created in some other class context but when it be created in
the same class context.
b) If the access specifier of the constructor is not private when an object of
corresponding class can be created in the context of same class and in another
class.
Why we are using constructors in our java program, it is mandatory to the java program to follow the following rules.
1. Constructor name must be similar to class name.
2. Constructor will be called implicitly whenever an object is created.
3. Constructor should not return any value even void as a return type (if we write
any return type then they acts as ordinary methods).
4. The modifier of the constructor should not be static (because constructors will be
called each and every time whenever an object is created).
5. Constructor never participates in inheritance classes that means constructor of
base class never inherited in to the context of derived class (because every class
constructor is meant of initialing its own data members but not meant for
initializing other class data members).
6. The access specifier of the constructor of a class may be private may not be
private.
a) If the access specifier of a constructor is private then object of a corresponding
class cannot be created in some other class context but when it be created in
the same class context.
b) If the access specifier of the constructor is not private when an object of
corresponding class can be created in the context of same class and in another
class.
Types of constructors: -
Based on creating an object in java constructors are classified in to two types.
They are
1. Default/parameter less/zero argument/no argument constructor
2. Parameterized constructor
Default Constructor: -
A constructor is said to be a default constructor if and only if never takes any parameters.
The purpose of default constructor is that to place same values in multiple objects of same class.
Syntax: -
Class <class name>
{
<class name> () // Default constructor
{
Block of statement (s);
Initialization;
}
}
Ex: -
Write a java program which illustrates the concept of default constructor?
class Demo
{
int a,b;
Demo() // programmer Define Default Constructor
{
System.out.println("i am from PDDC");
a=10;
b=20;
System.out.println("The value of a ="+a);
System.out.println("The value of b ="+b);
}
}
class DefaultDemo
{
public static void main(String[] args)
{
Demo d = new Demo();
Demo d1 = new Demo();
}
}
Based on creating an object in java constructors are classified in to two types.
They are
1. Default/parameter less/zero argument/no argument constructor
2. Parameterized constructor
Default Constructor: -
A constructor is said to be a default constructor if and only if never takes any parameters.
The purpose of default constructor is that to place same values in multiple objects of same class.
Syntax: -
Class <class name>
{
<class name> () // Default constructor
{
Block of statement (s);
Initialization;
}
}
Ex: -
Write a java program which illustrates the concept of default constructor?
class Demo
{
int a,b;
Demo() // programmer Define Default Constructor
{
System.out.println("i am from PDDC");
a=10;
b=20;
System.out.println("The value of a ="+a);
System.out.println("The value of b ="+b);
}
}
class DefaultDemo
{
public static void main(String[] args)
{
Demo d = new Demo();
Demo d1 = new Demo();
}
}
Rule 1: -
Whenever we create an object only with default constructor defining the default constructor is optional.
a) If we are not defining any default constructor then JVM will call its own default
constructor known as System Defined Default Constructor and it places default
values depends on the data members of the class.
b) If we define our own default constructor known as Programmer Defined Default
Constructor and it always places programmer defined values.
Note: -
We know that constructors are always meant for initializing the data members of their class. If we are not initialing the data members of the class then the data members of the class contains Default values.
Parameterized constructor: -
A constructor is said to be parameterized if and only if it always takes parameters
The purpose of parameterized constructors is that to create multiple objects of same class for placing different values either of same type (or) different type (or) both the types.
Syntax: -
Class <class name>
{
<class name> (list of formal parameters) // Parameterized constructor
{
Block of statement (s);
Initialization;
}
}
Ex: -
Write a java program which illustrates the concept of parameterized constructor?
class Demo
{
int a,b;
Demo(int x, int y) // Double Parameterized constructor
{
System.out.println("i am from PDDC");
a=x;
b=y;
System.out.println("The value of a ="+a);
System.out.println("The value of b ="+b);
}
}
class DefaultDemo
{
public static void main(String[] args)
{
Demo d = new Demo(10,20);
Demo d1 = new Demo(100,200);
}
}
Whenever we create an object only with default constructor defining the default constructor is optional.
a) If we are not defining any default constructor then JVM will call its own default
constructor known as System Defined Default Constructor and it places default
values depends on the data members of the class.
b) If we define our own default constructor known as Programmer Defined Default
Constructor and it always places programmer defined values.
Note: -
We know that constructors are always meant for initializing the data members of their class. If we are not initialing the data members of the class then the data members of the class contains Default values.
Parameterized constructor: -
A constructor is said to be parameterized if and only if it always takes parameters
The purpose of parameterized constructors is that to create multiple objects of same class for placing different values either of same type (or) different type (or) both the types.
Syntax: -
Class <class name>
{
<class name> (list of formal parameters) // Parameterized constructor
{
Block of statement (s);
Initialization;
}
}
Ex: -
Write a java program which illustrates the concept of parameterized constructor?
class Demo
{
int a,b;
Demo(int x, int y) // Double Parameterized constructor
{
System.out.println("i am from PDDC");
a=x;
b=y;
System.out.println("The value of a ="+a);
System.out.println("The value of b ="+b);
}
}
class DefaultDemo
{
public static void main(String[] args)
{
Demo d = new Demo(10,20);
Demo d1 = new Demo(100,200);
}
}
Rule 3: -
Whenever we create an object with parameterized constructor, defining the parameterized constructor is mandatory otherwise we get compile time error.
Rule 4: -
Whenever we create multiple objects with both default and parameterized constructors then it is mandatory to the java programmer to define both default and parameterized constructors otherwise we get compile time error.
Overloaded Constructor:-
A constructor is said to be overloaded if and only if constructor name is same but its signature is different
Signature represents the following points
1. Number of parameters
2. Type of parameters
3. Order of parameters
Ex: -
Test t1= new Test(10, 20); -->1
Test t2= new Test(100); -->2
Test t3= new Test(10.5f, 20.5f); -->3
Test t4= new Test(10.5f, 20); -->4
Test t5= new Test(10, 20.5f); -->5
Here Test(---) is known as Overloaded Constructor.
Object Parameterized constructor:-
A constructor is said to be object parameterized if and only if it must always takes object as a parameter.
The basic aim of object parameterized constructor is to copy the content of one object in to another object where both the objects belong to same type.
The functionality of object parameterized constructor exactly resembles (looking like same) the functionality of copy constructor concept of c++.
Ex:-
Write a java program which illustrates the concept of default constructor, parameterized constructor, and overloaded constructor and object parameterized constructor.
class Test3
{
int a,b;
Test3() // Default Constructor
{
System.out.println("i am from Default Constructor:");
a=1;
b=2;
System.out.println("The value of a="+a);
System.out.println("The value of b="+b);
}
Test3(int x,int y)// Double parameterized constructor
{
System.out.println("i am from Double Parameterized Constructor:");
a=x;
b=y;
System.out.println("The value of a="+a);
System.out.println("The value of b="+b);
}
Test3(int x)// Single parameterized constructor
{
System.out.println("i am from Single Parameterized Constructor:");
a=x;
b=x;
System.out.println("The value of a="+a);
System.out.println("The value of b="+b);
}
Test3(Test3 x)// Object parameterized constructor
{
System.out.println("i am from Object Parameterized Constructor:");
a=x.a;
b=x.b;
System.out.println("The value of a="+a);
System.out.println("The value of b="+b);
}
}
class TestDemo3
{
public static void main(String[] args)
{
Test3 t1= new Test3();
Test3 t2= new Test3(10,20);
Test3 t3= new Test3(100);
Test3 t4= new Test3(t3);
}
}
Whenever we create an object with parameterized constructor, defining the parameterized constructor is mandatory otherwise we get compile time error.
Rule 4: -
Whenever we create multiple objects with both default and parameterized constructors then it is mandatory to the java programmer to define both default and parameterized constructors otherwise we get compile time error.
Overloaded Constructor:-
A constructor is said to be overloaded if and only if constructor name is same but its signature is different
Signature represents the following points
1. Number of parameters
2. Type of parameters
3. Order of parameters
Ex: -
Test t1= new Test(10, 20); -->1
Test t2= new Test(100); -->2
Test t3= new Test(10.5f, 20.5f); -->3
Test t4= new Test(10.5f, 20); -->4
Test t5= new Test(10, 20.5f); -->5
Here Test(---) is known as Overloaded Constructor.
Object Parameterized constructor:-
A constructor is said to be object parameterized if and only if it must always takes object as a parameter.
The basic aim of object parameterized constructor is to copy the content of one object in to another object where both the objects belong to same type.
The functionality of object parameterized constructor exactly resembles (looking like same) the functionality of copy constructor concept of c++.
Ex:-
Write a java program which illustrates the concept of default constructor, parameterized constructor, and overloaded constructor and object parameterized constructor.
class Test3
{
int a,b;
Test3() // Default Constructor
{
System.out.println("i am from Default Constructor:");
a=1;
b=2;
System.out.println("The value of a="+a);
System.out.println("The value of b="+b);
}
Test3(int x,int y)// Double parameterized constructor
{
System.out.println("i am from Double Parameterized Constructor:");
a=x;
b=y;
System.out.println("The value of a="+a);
System.out.println("The value of b="+b);
}
Test3(int x)// Single parameterized constructor
{
System.out.println("i am from Single Parameterized Constructor:");
a=x;
b=x;
System.out.println("The value of a="+a);
System.out.println("The value of b="+b);
}
Test3(Test3 x)// Object parameterized constructor
{
System.out.println("i am from Object Parameterized Constructor:");
a=x.a;
b=x.b;
System.out.println("The value of a="+a);
System.out.println("The value of b="+b);
}
}
class TestDemo3
{
public static void main(String[] args)
{
Test3 t1= new Test3();
Test3 t2= new Test3(10,20);
Test3 t3= new Test3(100);
Test3 t4= new Test3(t3);
}
}
By default every class of java contains a System Defined Default constructor programmatically a class of java contains two categories of constructors they are
a) Default constructor
b) Multiple parameterized constructors(over loaded constructor)
Data members, constructors and methods of a class/Interface is known as profile. In order to view the profile of a class/Interface we use a command called JavaP, javap is one of the exe file/Application program/tool developed by sun Micro System and supplied as a part of jdk/bin folder.
Syntax: -
Javap <class name>/Interface
Ex: -
a) Default constructor
b) Multiple parameterized constructors(over loaded constructor)
Data members, constructors and methods of a class/Interface is known as profile. In order to view the profile of a class/Interface we use a command called JavaP, javap is one of the exe file/Application program/tool developed by sun Micro System and supplied as a part of jdk/bin folder.
Syntax: -
Javap <class name>/Interface
Ex: -
Private future of a class cannot be listed as a part of profiling.
Sum of the classes of java does not contain constructors (false).
‘this’ keyword: -
‘this’ is one of the keyword/object implicitly created by JVM and supplied to each and every java program for two purposes.
1. It always points to current class object.
2. Whenever the formal parameter and Data members of the class are similar then JVM
gets an ambiguity (no clarity in identifying).
In order to differentiate the formal parameter and data members of the class, the data members of the class must be preceded by a keyword ‘this’.
Syntax: -
this.<current class Data member>
Ex: -
Sum of the classes of java does not contain constructors (false).
‘this’ keyword: -
‘this’ is one of the keyword/object implicitly created by JVM and supplied to each and every java program for two purposes.
1. It always points to current class object.
2. Whenever the formal parameter and Data members of the class are similar then JVM
gets an ambiguity (no clarity in identifying).
In order to differentiate the formal parameter and data members of the class, the data members of the class must be preceded by a keyword ‘this’.
Syntax: -
this.<current class Data member>
Ex: -
Write a java program which illustrates the concept of ‘this’ keyword?
class Test4
{
int a; int b;
Test4(int a,int b)
{
this.a=a; // assigining the formal parameters
this.b=b;
a=a+1; // modifiying the values of formal parameters
b=b+1;
this.a=this.a+2; // modifiying the values of Data members
this.b=this.b+2;
System.out.println("The value of a="+a); // Here a, b refers formal parameters
System.out.println("The value of b="+b);
}
void disp()
{
System.out.println("The value of a="+a); // Here writing 'this' is optional
System.out.println("The value of b="+b);
}
};
class TestDemo4
{
public static void main(String[] args)
{
int x=Integer.parseInt(args[0]);
int y=Integer.parseInt(args[1]);
Test4 t1 = new Test4(x,y);//passing Dynamic values to parameterized constructor
t1.disp();
}
}
class Test4
{
int a; int b;
Test4(int a,int b)
{
this.a=a; // assigining the formal parameters
this.b=b;
a=a+1; // modifiying the values of formal parameters
b=b+1;
this.a=this.a+2; // modifiying the values of Data members
this.b=this.b+2;
System.out.println("The value of a="+a); // Here a, b refers formal parameters
System.out.println("The value of b="+b);
}
void disp()
{
System.out.println("The value of a="+a); // Here writing 'this' is optional
System.out.println("The value of b="+b);
}
};
class TestDemo4
{
public static void main(String[] args)
{
int x=Integer.parseInt(args[0]);
int y=Integer.parseInt(args[1]);
Test4 t1 = new Test4(x,y);//passing Dynamic values to parameterized constructor
t1.disp();
}
}
Write a java program which will compute addition of two objects?
class Test5
{
int a,b;
Test5()
{
a=b=0;
}
Test5(int a,int b)
{
this.a=a;
this.b=b;
}
void sum(Test5 x,Test5 y)
{
this.a=x.a+y.a;
this.b=x.b+y.b;
}
void disp()
{
System.out.println("the value of a ="+a);
System.out.println("the value of b ="+b);
}
}
class ObjSum
{
public static void main(String[] args)
{
if (args.length!=4)
{
System.out.println("please enter 4 values");
}
else
{
int x1=Integer.parseInt(args[0]);
int x2=Integer.parseInt(args[1]);
int x3=Integer.parseInt(args[2]);
int x4=Integer.parseInt(args[3]);
Test5 t1= new Test5(x1,x2);
Test5 t2= new Test5(x3,x4);
Test5 t3= new Test5();
t3.sum(t1,t2);
System.out.println("t1 values");
t1.disp();
System.out.println("t2 values");
t2.disp();
System.out.println("object sum");
t3.disp();
}
}
}
class Test5
{
int a,b;
Test5()
{
a=b=0;
}
Test5(int a,int b)
{
this.a=a;
this.b=b;
}
void sum(Test5 x,Test5 y)
{
this.a=x.a+y.a;
this.b=x.b+y.b;
}
void disp()
{
System.out.println("the value of a ="+a);
System.out.println("the value of b ="+b);
}
}
class ObjSum
{
public static void main(String[] args)
{
if (args.length!=4)
{
System.out.println("please enter 4 values");
}
else
{
int x1=Integer.parseInt(args[0]);
int x2=Integer.parseInt(args[1]);
int x3=Integer.parseInt(args[2]);
int x4=Integer.parseInt(args[3]);
Test5 t1= new Test5(x1,x2);
Test5 t2= new Test5(x3,x4);
Test5 t3= new Test5();
t3.sum(t1,t2);
System.out.println("t1 values");
t1.disp();
System.out.println("t2 values");
t2.disp();
System.out.println("object sum");
t3.disp();
}
}
}
In the above program the statement t3=t1+t2 is invalid because ‘+’ operator cannot be applied (ant operator) between objects and arrays because java programming will not support operator overloading facility of c++, but the functionality of operator overloading of c++ achieved in java programmatically through the concept of methods