Btechadda.com

OOPS/JAVA:Constructors

by hkesavaraju on Feb.27, 2010, under Java

Constructors: It is tedious to initialize all the variables of a class when each time an instance is created or also using methods in a class. This work made simpler by placing all of the set up done at the time the object is first created. Java allows objects to initialize themselves when they are created. This automatic initialization is performed through the use of a constructor.

A constructor has same name as class name in which it resides and is syntactically similar to a method. Once defined, constructor is called immediately after the creation of its object. Constructors have no return type, not even void because the implicit return type of a class constructor is the class type itself.

Properties of constructor:

  1. It can be invoked when its class object is created first time.

  2. It don’t have return type.

  3. It is syntactically similar to a method.

  4. It has same name as its class name.

The following program illustrates the constructor which is invoked automatically when an object is constructed.

class box
{
double w,h,d;
box()
{
System.out.println(“constructing box”);
w=h=d=10;
}
double volume()
{
return w*h*d;
}
class boxdemo
{
public static void main(String args[])
{
box m1=new box();
box m2=new box();
double vol;
vol=m1.volume();
System.out.println(“volume is”+vol);
vol=m2.volume();
System.out.println(“volume is”+vol);
}
}

Output:

constructing box

constructing box

volume is 1000.0

volume is 1000.0

In above code, m1 & m2 were initialized by box constructor when they are created.

The syntax for calling the constructor by the creation of object is

class-var=new classname();

The parentheses are needed after the classname because constructor for the class is called.

2.11.1 Parameterized constructors: These are constructors but takes parameters as arguments.

This concept is needed when a way to create objects of various dimensions. The following program illustrates parameterized constructors.

class box
{
double w,h,d;
box(double w1,double h1,double d1)
{
w=w1;
h=h1;
d=d1;
}
double volume()
{
return w*h*d;
}
}
class boxdemo
{
public static void main(String args[])
{
box m1=new box(2,3,4);
box m2=new box(10,20,30);
double vol;
vol=m1.volume();
System.out.println(“volume of  m1 is”+vol);
vol=m2.volume();
System.out.println(“volume of  m2 is”+vol);
}
}

Output:

volume of m1 is 24.0

volume of m2 is 6000.0

In the above code, the creation of m1 object invokes box(2,3,4) constructor which in turn assigns 2,3,4 to box variables w, h & d. Similarly, the creation of m2 object invokes box(10,20,30) constructor which assigns 10,20 & 30 to box variables w, h & d respectively.

2.11.2 Constructor overloading: Like overloading normal methods, constructor methods can also be overloaded. It means several constructors in the same class has same name but differ in the number and type of arguments.

Observe a situation where the class box has a constructor as box(double w1,double h1,double d1), then the object declaration should take three parameters, then only the constructor in box is called.

Suppose the object declaration like box ob=new box(); is done, it raises an error and it will not call box() constructor with three arguments.

Java allows constructors to overload in the same class which solves these problems.

The following program illustrates constructor overloading:

class box
{
double w,h,d;
box()
{
w=h=d=-1;
}
box(double w1,double h1,double d1)
{
w=w1;
h=h1;
d=d1;
}
box(double len)
{
w=h=d=len;
}
double volume()
{
return w*h*d;
}
}
class oconstructordemo
{
public static void main(String args[])
{
box m1=new box(2,3,4);
box m2=new box();
box m3=new box(4);
double vol;
vol=m1.volume();
System.out.println(“volume of m1 is”+vol);
vol=m2.volume();
System.out.println(“volume of m1 is”+vol);
vol=m3.volume();
System.out.println(“volume of m1 is”+vol);
}
}

Output:

volume of m1 is 24.0

volume of m1 is -1.0

volume of m1 is 64.0

In the above code, the proper constructor is called based on parameters passed in the object declaration.


Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Blogroll

A few highly recommended websites...