Java

TOPIC 37 – CONSTRUCTORS II

 

 

LESSON NOTE

 

 

MULTIPLE CONSTRUCTORS

 

  • We can have more than one constructor in class.  This allows us to give more options on how to create an object.

  • This is just like function overloading.  We can call this constructor overloading. When doing this, no two constructors can have two identical argument lists.

  • When we create an object, the constructor that has the matching argument list is called.

 

EXAMPLE – TWO CONSTRUCTORS IN THE POINT CLASS

 

In this example, we will add a 2nd constructor to the Point class that we looked at last lesson.

The new constructor has no arguments.  It will automatically create a point that is at (0,0).

 

     public Point()

     {

         x = 0.0;

         y = 0.0;

     }

 

The entire Point class now looks like this:

 

     public class Point

     {

        public double x;

        public double y;

 

        public Point(double tmpx, double tmpy)

        {

            x = tmpx;   //setting x data field

            y = tmpy;   //setting y data field

        }

 

        public Point()

        {

            x = 0.0;

            y = 0.0;

        }

      }

 

From a main method in another class, we can now create a Point object using either constructor.

 

     public static void main(String[] args)
     {

        Point t = new Point(3.2, 5.2);  //using first constructor

        Point z = new Point();          //using second constructor

       

        System.out.println(t.x);

        System.out.println(t.y);

 

        System.out.println(z.x);

        System.out.println(z.y);

     }

 

After the two above statements, the objects would look like this in memory:

 

 



EXAMPLE (RECTANGLE CLASS)

 

In this example, we will create a Rectangle class that will have four different constructors.  Rectangles of this class are defined by the x and y coordinates of their bottom left and top right points.  So, there are four data fields named x1, y1, x2, y2.


Here are the class guidelines:

·                              It will have four double data fields named x1, y1, x2, y2.

·                              Constructor #1 has four double arguments, one for each data field.

·                              Constructor #2 has no arguments.  It will create a Rectangle with opposite corners (0,0) and (1,1).

·                              Constructor #3 has two double arguments.  The arguments will be the values for x2 and y2.  The value of (x1, y1) will simply be (0,0).

·                              Constructor #4 has three double arguments.  The first two arguments will be values for x1 and y1.  The third argument will be both the height and the width of the rectangle (square).  We have to calculate the value of x2 and y2 based on this.

 

public class Rectangle

{

   public double x1;

   public double y1;

   public double x2;

   public double y2;

  

   //CONSTRUCTOR #1

   public Rectangle(double mx1, double my1, double mx2, double my2)

   {

      x1 = mx1;

      y1 = my1;

      x2 = mx2;

      y2 = my2;

   }

  

   //CONSTRUCTOR #2

   public Rectangle()

   {

      x1 = 0.0;

      y1 = 0.0;

      x2 = 1.0;

      y2 = 1.0;     

   }

  

   //CONSTRUCTOR #3

   public Rectangle(double mx2, double my2)

   {

      x1 = 0.0;

      y1 = 0.0;

      x2 = mx2;

      y2 = my2;     

   }

  

   //CONSTRUCTOR #4

   public Rectangle(double mx1, double my1, double side)

   {

     x1 = mx1;

     y1 = my1;

     x2 = mx1 + side;

     y2 = my1 + side;

   }

}

 

We can now create Rectangle objects using any of the four constructors.  Here is an example of creating an object with each one of the constructors.

 

             Rectangle r1 = new Rectangle(0.0, 8.0, 10.5, 9.0); //1st constructor

             Rectangle r2 = new Rectangle(); //2nd constructor

             Rectangle r3 = new Rectangle(12.4, 8.2); //3rd constructor

             Rectangle r4 = new Rectangle(2.0, 3.0, 6.0); //4th constructor

 

The object r1 would look like this in memory:

 

 

 


DEFAULT CONSTRUCTOR

 

Question:

Before we learned about constructors, the classes did not have any constructors.  But, we are now learning that constructors are needed to create objects – so how come we were able to create simple objects without having our own constructor?

Answer:

When a class is not given a constructor, Java provides a default constructor that has no arguments.  It automatically initializes data fields of a specific type to some value.

·                              All int data fields are set to 0.

·                              All double data fields are set to 0.0.

·                              All boolean data fields are set to false.

·                              All char data fields are set to a space.

·                              All objects (including Strings) are set to null.

Because the default constructor might be setting data fields to illegal (or undesired) values, it is almost always better to provide your own constructor.

DEFAULT CONSTRUCTOR AVAILABILITY

It is important to note that the default constructor is only usable if no constructor is provided at all.  If a constructor with arguments is provided, then one cannot use the no-argument default constructor as another option.

 

EXAMPLE – DEFAULT CONSTRUCTOR

 

Below are two classes that show what the default constructor does.  The TestObject class simply has five types of data fields that will all get initialized by the default constructor (because no other constructor is provided.).  The Test class simply creates an object and outputs the value of each data field to screen.

 

Here is the TestObject class:

 

public class TestObject

{

   public int a;

   public double b;

   public char c;

   public boolean d;    

   public String e; 

}

 

Here is the main function that tests what the default constructor initialized the data fields to.

 

public class Tester

{

   public static void main(String[] args)

   {

      TestObject t = new TestObject();

      System.out.println(t.a);

      System.out.println(t.b);

      System.out.println(t.c);

      System.out.println(t.d);

      System.out.println(t.e);    

   }

}

 

The code above will output the following to screen:

 

0

0.0

 

false

null

 

 

INTRODUCING THIS

 

From inside the class itself, we can refer to the object by using this.

 

So, we can specifically use this.datafield to refer to the datafields of the object.



EXAMPLE

 

The following Point class makes use of this in front of datafields.

 

public class Point

{

     public double x;

     public double y;

 

     public Point (double x, double y)
     {

          this.x = x;

          this.y = y;

     }

}

 

Note that using the this name allows Java to differentiate between the local variables x and y and the datafields x and y. 

 

Of course, it may be easier to avoid confusion and use different names for the datafields and the local variables.  We often use “tmp” at the start of the local variable when we are starting to code in OOP in order to remember that the local variables will be erased as soon as the constructor is done executing.