Friday, June 12, 2015

CONSTRUCTORS in java

11:49 PM

CONSTRUCTORS in java

A constructor is a special member method which will be called by the JVM implicitly
(automatically) for placing user/programmer defined values instead of placing default values.
Constructors are meant for initializing the object.
ADVANTAGES of constructors:
1. A constructor eliminates placing the default values.
2. A constructor eliminates calling the normal method implicitly.
RULES/PROPERTIES/CHARACTERISTICS of a constructor:
1. Constructor name must be similar to name of the class.
2. Constructor should not return any value even void also (if we write the return type for the
constructor then that constructor will be treated as ordinary method).
3. Constructors should not be static since constructors will be called each and every time
whenever an object is creating.
4. Constructor should not be private provided an object of one class is created in another class
(constructor can be private provided an object of one class created in the same class).
5. Constructors will not be inherited at all.
6. Constructors are called automatically whenever an object is creating.

Based on creating objects in JAVA we have two types of constructors. They are
default/parameter less/no argument constructor and parameterized constructor.
• A default constructor is one which will not take any parameters.
Syntax:
class <clsname>
{
clsname () //default constructor
{
Block of statements;
………………………………;
………………………………;
}
………………………;
………………………;
};

For example:
class Test
{
int a, b;
Test ()
{
System.out.println ("I AM FROM DEFAULT CONSTRUCTOR...");
a=10;
b=20;
System.out.println ("VALUE OF a = "+a);
System.out.println ("VALUE OF b = "+b);
}
};
class TestDemo
{
public static void main (String [] args)
{
Test t1=new Test ();
}
};

Note:
Whenever we create an object only with default constructor, defining the default
constructor is optional. If we are not defining default constructor of a class, then JVM will call
automatically system defined default constructor (SDDC). If we define, JVM will call
user/programmer defined default constructor (UDDC).

• A parameterized constructor is one which takes some parameters.
Syntax:
class <clsname>
{
…………………………;
…………………………;
<clsname> (list of parameters) //parameterized constructor
{
Block of statements (s);
}
…………………………;
…………………………;
}
For example:
class Test
{
int a, b;
Test (int n1, int n2)
{
System.out.println ("I AM FROM PARAMETER CONSTRUCTOR...");
a=n1;
b=n2;
System.out.println ("VALUE OF a = "+a);
System.out.println ("VALUE OF b = "+b);
}
};
class TestDemo1
{
public static void main (String k [])
{
Test t1=new Test (10, 20);
}
};

RULE-2:
Whenever we create an object using parameterized constructor, it is mandatory for the
JAVA programmer to define parameterized constructor otherwise we will get compile time error.
• Overloaded constructor is one in which constructor name is similar but its signature is
different. Signature represents number of parameters, type of parameters and order of
parameters. Here, at least one thing must be differentiated.
For example:
Test t1=new Test (10, 20);
Test t2=new Test (10, 20, 30);
Test t3=new Test (10.5, 20.5);
Test t4=new Test (10, 20.5);
Test t5=new Test (10.5, 20);
RULE-3:
Whenever we define/create the objects with respect to both parameterized constructor and
default constructor, it is mandatory for the JAVA programmer to define both the constructors.
NOTE:
When we define a class, that class can contain two categories of constructors they are single
default constructor and ‘n’ number of parameterized constructors (overloaded constructors).

Write a JAVA program which illustrates the concept of default constructor, parameterized
constructor and overloaded constructor?


Answer:

class Test
{
int a, b;
Test ()
{
System.out.println ("I AM FROM DEFAULT CONSTRUCTOR...");
a=1;
b=2;
System.out.println ("VALUE OF a ="+a);
System.out.println ("VALUE OF b ="+b);
}
Test (int x, int y)
{
System.out.println ("I AM FROM DOUBLE PARAMETERIZED CONSTRUCTOR...");
a=x;
b=y;
System.out.println ("VALUE OF a ="+a);
System.out.println ("VALUE OF b ="+b);
}
Test (int x)
{
System.out.println ("I AM FROM SINGLE PARAMETERIZED CONSTRUCTOR...");
a=x;
b=x;
System.out.println ("VALUE OF a ="+a);
System.out.println ("VALUE OF b ="+b);
}
Test (Test T)
{
System.out.println ("I AM FROM OBJECT PARAMETERIZED CONSTRUCTOR...");
a=T.a;
b=T.b;
System.out.println ("VALUE OF a ="+a);
System.out.println ("VALUE OF b ="+b);
}
};
class TestDemo2
{
public static void main (String k [])
{
Test t1=new Test ();
Test t2=new Test (10, 20);
Test t3=new Test (1000);
Test t4=new Test (t1);
}
};

Written by

We are Creative Blogger Theme Wavers which provides user friendly, effective and easy to use themes. Each support has free and providing HD support screen casting.

0 comments:

Post a Comment

 

© 2013 Java Tutorials. All rights resevered. Designed by Templateism

Back To Top