Monday, December 13, 2010

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();
}
}

4 comments:

  1. Please tell why this gives error :-

    import java.util.Scanner;
    public class YO {
    public void main() {
    Scanner in=new Scanner(System.in);
    int e=in.nextInt();
    int f=in.nextInt();
    YO c=new YO(e,f);
    c.disp();
    }
    public YO(int a,int b) {
    int c=a;int d=b;
    }
    public void disp() {
    System.out.println("Sum="+(a+b));
    }
    }

    It says it cant find a & b .

    ReplyDelete
    Replies
    1. Both a and b are declared in constructor, means they are not data members of the object,

      local members of constructor. You can not access local members in other function. You are getting error from:
      public void disp() {
      System.out.println("Sum="+(a+b));
      }

      Delete
  2. This program dont gives any error while compiling but when i run , its virtual machine (at bottom left starts moving) and keeps on moving-it gets hanged up. Why ?

    import java.util.Scanner;


    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[])
    {
    Scanner in=new Scanner(System.in);
    int yo,yo2;
    yo=in.nextInt();
    yo2=in.nextInt();
    Ab ob=new Ab(yo,yo2);
    ob.display();
    }
    }

    ReplyDelete
    Replies
    1. Put 'System.out.println("Enter number:");' before yo=in.nextInt(); , yo2=in.nextInt(); and then execute the program. There is nothing wrong in the program.

      Delete

Subscribe via email

Enter your email address:

Delivered by FeedBurner