object parameterized constructor constructor with object parameter object parameterized constructor in java object parameterized constructor in java example object parameterized constructor in java program
copy constructor in java copy constructor in java with example copy constructor in java example
copy constructor in java tutorial example program for copy constructor in java
Object Parameterized Constructor:
A Constructor is said to be object parameterized if and only if it takes object as
parameter.
By using this concept is to copy the content of one object into another
object.
The Concept of object parameterized constructor exactly similar to the concept
of
copy constructor of C++. In other words copy constructor concept doesnot support by java.but we can
obtained the
functionality of copy constructor by using object parameterized
constructor.
Following Program illustrates the concept of default overloaded and object
parameterized
constructor.
For Example:
Class Consdemo2
{
int a,b;
Consdemo2()// Default Constructor
{
System.out.println(“default
parameterized Constructor”);
a=1;
b=2;
System.out.println(“value
of a”+a);
System.out.println(“value
of b”+b);
}
Consdemo2(int x,int y)// Double Parameterized Constructor
{
System.out.println(“double
parameterized Constructor”);
a=x;
b=y;
System.out.println(“value of
a”+a);
System.out.println(“value of
b”+b);
}
Consdemo2(Consdemo2 X)//Formal Parameter X Of Consdemo2 Class
{
System.out.println(“Object
parameterized Constructor”);
a=X.a;
b=X.b;
System.out.println(“value of
a”+a);
System.out.println(“value of
b”+b);
}
Class MainDemo
{
Public static void main(String args[])
{
Consdemo2 c1=new Consdemo2();
Consdemo2 c2=new Consdemo2(10,20);
Consdemo2 c3=new Consdemo2(c2);//Object Parameterized
Calling constructor Or We can call it as
//new Consdemo2(c2)
}
}
Analaysis:
In the above program Object c3 storing value that are stored in the Object c2 of values are
10,20.
Consdemo2(Consdemo2 X) formal parameter of X of type
ConsDemo2, then X it access
as X.a and X.b.
OutPut:
Default parameterized constructor
1
2
Double parameterized constructor
10
20
Object parameterized constructor
10
20
No comments:
Post a Comment