Monday, 4 April 2016

Parameterized Constructor in Java

Know About Parameterized Constructor in Java........

Parameterized Constructor: - a constructor is said to be parameterized if and only if constructor always takes parameters.
The purpose of parameterized constructor is that it always takes parameters and creates multiple objects with respect to same class for placing different values.
Syntax:-                 class <clsname>
                                 {
                                       ……………
                                      …………….
                                  <clsname>(list of formal parameters)
                                      {
                                        Block of statements(s)……….initialize
                                    }
                                   …………………
                                   …………………
}

Write a java program which illustrates the concept of parameterized constructor.
class Constructor2
{
            int a,b;
            Constructor2(int x,int y)
            {
                        System.out.println("Constructor..........DPC");
                        a=x;
                        b=y;
                        System.out.println("The value of a is="+a);
                        System.out.println("The value of b is="+b);
            }
}



class Constructor2Demo
{
            public static void main(String k[])
            {
                        Constructor2 c1=new Constructor2(10,20);
                        Constructor2 c2=new Constructor2(100,200);
                        Constructor2 c3=new Constructor2(1000,2000);
                       
            }
}

Rule2:- when we create an object with parameterized constructor then defining the parameterized constructor is mandatory otherwise we get compile time error.
Rule3:- whenever we create multiple objects with both default & parameterized constructor then it is mandatory to the java programmer to define both default & parameterized constructors otherwise we get compile time error.
Note: - if we are not initializing data members of any class with respect to corresponding class constructors then the uninitialized data members will be initialized by system defined default constructor with default values.
Overloaded Constructor: - a constructor is said to be overloaded if and only if constructor name is same but signature is different.
Signature represents the following point…….
No. of parameters
Type of parameters
Order of parameter
At least one thing must be different
Test t1=new Test(10,20);
Test t2=new Test(100);
Test t3=new Test(1.5f,2.5f);
Test t4=new Test(10,1.5f);
Test t5=new Test(1.5f,20);
Here Test(. . .) is known of overloaded constructor.
Object parameterized constructor:- a constructor is said to be object parameterized if and only if it always takes object as a parameter.
The purpose of object parameterized constructor is that to copy the content of one object into another object belongs to same type.
Note: - the functionality of object parameterized constructor resembles the copy constructor of c++.



No comments:

Post a Comment