Wednesday, February 23, 2011

Difference between constructor and function in BlueJ

Difference between constructor and function in BlueJ

Constructor is called a special function due to many reasons. The points which make them different are listed below.

(i) Class name and it’s constructor name must be same where as class name and function name must be different.

(ii) Constructor has no return type not even void, function should have a return type. If the function does not return any value, it should be void.

(iii) Constructor can’t use any return statement within it’s body. Return type function have to use return statement. The return statement of a return type function should be followed by a value or expression which results in a value which matches the return type of the function. Void type function can also use return statement but it should not be followed by any value. Such return statements are used in void type function to send the control back to the caller of the function before the execution of the function is completed.

(iv) Constructor execution is a must whereas all the functions defined in a class may not execute.

(v) If a constructor is not defined in a class, compiler supplies the default constructor – means constructor with zero / no argument and empty body. This is not the case with functions. Normally constructor initializes the data members of an object. In broader sense constructors are used to perform the loading stage jobs in a program.

Point number (v) needs some clarification specially for ISC students. Normal ICSE / ISC BlueJ programs can be done without defining any constructor. Then what is the need of defining constructor? Again if we define constructor in a class, why the entire job of the program is not performed within the constructor?

‘what is the need of defining constructor?’. As stated constructors are used to perform the loading stage jobs in a program apart from initializing the data members. Loading stage jobs means the jobs which are to be done at the start of the program and which is must. These type of job is not within the scope of ICSE or ISC students.


Lets discuss this matter using some BlueJ programs.

This is a simple BlueJ program of displaying the sum of two numbers.

Program number 1

This program uses two functions, the first function is public void set(int x,int y) which takes two values and sets them on the object members. The other function public void display() displays the sum of the values.

class Ab
{
int a,b;
public void set(int x,int y)
{
a=x;
b=y;
}
public void display()
{
System.out.println("Sum="+(a+b));
}
public static void main(String args[])
{
Ab ob=new Ab();
ob.set(2,3);
ob.display();
}
}

Program number 2 is little bit modified. In this program constructor is defined with argument to initialize the object members and the public void set() function is omitted as the job of this function is performed by the constructor.

class Ab
{
int a,b;
public Ab(int x,int y)
{
a=x;
b=y;
}
public void display()
{
System.out.println("Sum="+(a+b));
}
public static void main(String args[])
{
Ab ob=new Ab(2,3);
ob.display();
}
}


Program number 3 is the worst among the three programs. Here the entire job is done within the constructor body means whenever object is created it will display the result also.

class Ab
{
int a,b;
public Ab(int x,int y)
{
a=x;
b=y;
System.out.println("Sum="+(a+b));
}
public static void main(String args[])
{
Ab ob=new Ab(2,3);
}
}

This program is the best one with distinct job for different functions. Here in this program overloaded versions of constructor are used. One constructor creates the object and at the same time initialize the object members and the other just creates the object. The need of constructor overloading is related with inheritance which will be discussed in the next posting.

class Ab
{
int a,b;
public void set(int x,int y)
{
a=x;
b=y;
}
void display()
{
System.out.println("Sum="+(a+b));
}
public Ab(int x,int y)
{
a=x;
b=y;
}
public Ab()
{
}
public static void main(String args[])
{
Ab ob=new Ab(2,3);
ob.display();
Ab ob1=new Ab();
ob1.set(10,20);
ob1.display();
}
}

No comments:

Post a Comment