More on Object Oriented Programming


By now, you should have grasped the concepts dealing with object oriented programming. You know all about classes, objects, and set and get methods. Now, it's time to learn about constructors. This will be the final tutorial dealing with object oriented programming.

What is a Constructor

Remember the Car class we developed in the previous tutorials? Well, we had something like this to create an object of the class Car.

Car myCar = new Car();


The piece of code
Car()
is called the constructor of the class. Think of the constructor as a method that creates an object of the class it is in. It also can take parameters like a normal method. However, you may have noticed that in our Car class, we didn't code a constructor so let's do this now, check the code below.

public class Car
{
   private String engine;
   private double height;
   private double width;
   private String gear;
   private int model;
   private String make;

   public Car(String eng, double h, double w, String g, int mod, int mak)
   {
      engine = eng;
      height = h;
      width = w;
      gear = g;
      model = mod;
      make = make;
   }

   public void print()
   {
      System.out.println("The car specs are:\n");

      System.out.println("Engine: "+engine);

      System.out.println("Height: "+height);

      System.out.println("Width: "+Width);

      System.out.println("Gear: "+gear);

      System.out.println("Model: "+model);

      System.out.println("Make: "+make);
   }
}

The syntax of a constructor is

public classname(parameters if any)


You may have noticed this in the code above. We wrote a constructor for car and gave it the parameters to initialize the class variables. Let's take our Car class for a test run. Check the code below.

public class CarTest
{
   public static void main(String args[])
   {
      Car myCar = new Car("V12", 0.5, 2.5, "Manual", 2010, "Lamborghini");

      myCar.print();
   }
}

Output:

The car specs are:

Engine: V12
Height: 0.5
Width: 2.5
Gear: Manual
Model: 2010
Make: Lamborghini

The constructor of the class Car took the parameters from the CarTest class and initialized every variable to the parameter given. Then we printed the output to the console. Congrats, you own a Lamborghini now too :)

The Default Constructor

In our old Car class, we didn't have a constructor, so how did Java know what to construct and how to initialize the variables. The answer is simple, each class has a default constructor that doesn't need to be written in code. It will leave the variables with no values until a user manipulates them. Now, Let's modify our Car class to see what happens when we make the default constructor handle variables. Check the code below.

public class Car
{
   private String engine;
   private double height;
   private double width;
   private String gear;
   private int model;
   private String make;

   public Car()
   {
      engine = "Not Specified";
      height = 0;
      width = 0;
      gear = "Not Specified";
      model = 0;
      make = "Not Specified";
   }

   public Car(String eng, double h, double w, String g, int mod, int mak)
   {
      engine = eng;
      height = h;
      width = w;
      gear = g;
      model = mod;
      make = make;
   }

   public void print()
   {
      System.out.println("The car specs are:\n");

      System.out.println("Engine: "+engine);

      System.out.println("Height: "+height);

      System.out.println("Width: "+Width);

      System.out.println("Gear: "+gear);

      System.out.println("Model: "+model);

      System.out.println("Make: "+make);
   }
}

Now let's test this new Car class.

public class CarTest
{
   public static void main(String args[])
   {
      Car myCar = new Car();

      myCar.print();
   }
}

Output:

The car specs are:

Engine: Not Specified
Height: 0.0
Width: 0.0
Gear: Not Specified
Model: 0
Make: Not Specified

Using Methods in Constructors

Remember that a method can call another method, and since a constructor can be viewed as a method, it can call other methods too. When we initialized the class variables in the constructor, we didn't validate the values like the way we do in a set method. Therefore, let's write a method that validates the height and call it from the constructor.

public class Car
{
   private String engine;
   private double height;
   private double width;
   private String gear;
   private int model;
   private String make;

   public Car()
   {
      engine = "Not Specified";
      height = 0;
      width = 0;
      gear = "Not Specified";
      model = 0;
      make = "Not Specified";
   }

   public Car(String eng, double h, double w, String g, int mod, int mak)
   {
      engine = eng;
      validateHeight(h);
      width = w;
      gear = g;
      model = mod;
      make = make;
   }

   public void validateHeight(double h)
   {
      if(h <= 0)
         height = 0.0;
      else
         height = h;
   }

   public void print()
   {
      System.out.println("The car specs are:\n");

      System.out.println("Engine: "+engine);

      System.out.println("Height: "+height);

      System.out.println("Width: "+Width);

      System.out.println("Gear: "+gear);

      System.out.println("Model: "+model);

      System.out.println("Make: "+make);
   }
}

The method

validateHeight
will take as parameter a double and checks if it is less than zero. If so, it will assign to height the value zero, else it will assign the double to height. We called this method in the constructor to initialize height and this is perfectly correct.

Congratulations

You now know how to use constructors and how they function. We are now done with section of tutorials so move on to Advanced Methods, but first check the following important notes.

Important Notes

Validate with Constructors

Try to always use set methods with constructors intead of just giving a variable a value. This way, you will have correct input.