Showing posts with label Default Constructor Para metered constructor. Show all posts
Showing posts with label Default Constructor Para metered constructor. Show all posts

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++.



Wednesday, 30 March 2016

Constructors in java

Know About Constructors in java...........


Constructors in java:- a constructor is one of the special member method and it is automatically / implicitly called by JVM  during object creation time for placing our own values without placing default values.
Advantages of Constructors:-
When we develop the java application with the concept of constructors then such applications will get the following advantages-
·        Constructors eliminate default values.
·        Constructors eliminate in calling ordinary method.
In other word the purpose of constructor concept is to initialize the object.
Rules/Characteristic/properties:-
·        Constructor will be called implicitly/automatically during object creation time.
·        Each and every constructor name is similar to class name.
·        Constructor will not return any value even void also(if we write any return type then it will be treated as an ordinary method )
·        Constructor should not be static.(because whenever an object is created constructors will be called each & every time)
·        Constructors in java programming will not participate in inheritance process (because every constructor is mint for initializing its own class data members )
·        The access modifier of the constructor may or may not be private.
(a)  If the access modifier constructor is private an object of the corresponding class can be created in same class context of some other class.
(b) If the access modifier of the constructor is not private then we can create an object of corresponding class in the same class context and in another class context.

Types of constructors in java:-
 In java programming we have two types of constructors they are ….
·        Default/parameter less/zero argument constructor.
·        Para metered constructor.
1.     Default Constructor:- a constructor is said to be default if at only if it will not take any parameters.
The purpose of default constructor is to create multiple object with respect to same class for placing same values.
Syntax:-                       class <clsname>
                                      {
                                              <clsname>()
                                               {
                                                        Block of statements(s)………….initialization
                                                  }
                                             }
Ø Write a java program which illustrates the concept of default constructor.
  class Constructor
{
      int a,b;
      Constructor()
      {
                  System.out.println("Constructor...........DC");
                  a=1;
                  b=2;
                  System.out.println("the value of a="+a);
                  System.out.println("the value of b="+b);
      }
                 
}



class  ConstructorDemo
{
      public static void main(String k[])
      {
                  Constructor c1=new Constructor();
                  Constructor c2=new Constructor();
                  Constructor c3=new Constructor();

                 
      }
}


Note:- Whenever we create an object only with default constructor then defining the default construction is optional whenever we create an object with default constructor and if we are not defining default constructor then JVM will call its own constructor known as system defined default constructor & whose role is to place default values.

Rule1:- if we create an object with the default constructor and if we define the default constructor then JVM will execute programmer defined default constructor & places programmer defined values.