Monday, 16 November 2009

Constructors

We have seen earlier that constructor is used to create a new object
Student one = new Student ( ) ;
new gives a call to the constructor of class
But , we did not define any constructor so how was one found ?
Java compiler automatically adds an empty , no argument constructor
if none is provided by the programmer.

It's access level is same as that of the defining class

Defining constructors
Their declaration is similar to that of methods
General form
access level name ( parameter list )
{
// body of constructor
}
access level : public , protected , private
If none is specified , default access is used
name : It must be same as class name
parameter list : It is the list of type-identifier pairs seperated by , . These identifiers receive the value of arguments passed in a constructor call
body : here you can initialize the object or call other constructors

Diferences from methods

  • Methods can have any valid identifier as name but constructor name must be same as class name
  • Methods must specify return type whereas constructors only return the class type which is not specified.
  • No return statement is required , constructors themselves return class type
Like methods , they can also be parameterized and overloaded

example ( parametrized constructor)
Working on the earlier used Student example
Student one = new Student ( ) ;
This calls the no argument constructor provided by Java compiler
We can define our own constructors also
Student ( String input1 , int input2 )
{
name = input1 ;
age = input2 ;
}
This constructor defines two parameters
Student one = new Student ( "Harry" , 20) ;
As can be seen , object properties get initialized inside the constructor

Advantage of this approach is that for each Student object we create ,
we don't have to write these two statements
one.name = "studentname" ;
one.age = 25 ; // or any valid value

for each object we create .

It's better to write it once inside the constructor , and reuse it for all the objects created by passing the values for name and age .

example (constructor overloading)

  • Just as methods can be overloaded , same way constructors can be
    overloaded by changing the signature of different constructors
  • Appropriate constructor gets called by matching the arguments
    passed and the parameters of constructor.

    Student ( ) // no parameter
    {
    }
    Student ( String input1 ) // one parameter
    {
    name = input1 ;
    }
    Student ( String input1 , int input2 ) // two parameters
    {
    name = input1 ;
    age = input2 ;
    }
    new Student ( ) ; // calls the first constructor
    new Student ( "Harry" , 20) ; // calls the third constructor

    Why overloading is required ?
    Sometimes , we don't have values for all properties of an Object at the
    time we are creating that object
    Say , we have a student whose age is not known initially
    Student one = new Student ( "Mike" ) ; // calls the second constructor
    This student has name Mike but no age
    When age is known , we can set age
    one.age = 25 ;
    This brings flexibility in initializing objects