CodingBison

We have already seen the basic OOP concepts like class and object in our earlier sections. Going further, we will deep dive into the other object oriented programming concepts. Let us get introduced to the concept of "Constructor" by re-writing the example program used in the "Class" section of this module.

 class Movie {
     String name;
     int yearOfRelease;

     Movie() {
         System.out.println(" Inside the default constructor ");
         name = "Jame Bond Movie";
         yearOfRelease = 0000;
     }
     Movie(String name, int yearOfRelease) {
         System.out.println(" Inside the parameterized constructor,"
                         + " initializing the object, Movie Name: " + name);
         this.name = name;
         this.yearOfRelease = yearOfRelease;
     }

     void setName(String movieName) {
         name = movieName;
     }

     String getName() {
         return name;
     }	

     void setYearOfRelease(int movieYearOfRelease) {
         yearOfRelease = movieYearOfRelease;
     }

     int getYearOfRelease() {
         return yearOfRelease;
     }	
 }

 /* Name this source file as "TestMovieClass.java". */
 public class TestMovieClass {
     public static void main(String[] args) {
         Movie movie1 = new Movie("Dr.No", 1962);
         Movie movie11 = new Movie("Moonraker", 1979);
         Movie movie23 = new Movie("Skyfall", 2012);
         Movie movie24 = new Movie();

         String movieName;
         int    movieYearOfRelease;

         movieName = movie1.getName();
         movieYearOfRelease = movie1.getYearOfRelease();

         System.out.println(" 1st James Bond Movie Name: " + movieName +
                            ", Year of Release: " + movieYearOfRelease);

         movieName = movie11.getName();
         movieYearOfRelease = movie11.getYearOfRelease();

         System.out.println(" 11th James bond Movie Name: " + movieName +
                            ", Year of Release: " + movieYearOfRelease);

         movieName = movie23.getName();
         movieYearOfRelease = movie23.getYearOfRelease();

         System.out.println(" 23rd James bond Movie Name: " + movieName +
                            ", Year of Release: " + movieYearOfRelease);

         /* Let us say we know there would be 24th James bond movie
          * but none of the details are know.
          * We create an object with default values. 
          */
         movieName = movie24.getName();
         movieYearOfRelease = movie24.getYearOfRelease();

         System.out.println(" \n Default Constructor (24th James" +
         		   " bond movie, details not know yet) \n \t"  +
                            " James bond Movie Name: " + movieName +
                            ", Year of Release: " + movieYearOfRelease);
         /* Set the values after the details are known */
         movie24.setName("Spectre");
         movie24.setYearOfRelease(2015);

         movieName = movie24.getName();
         movieYearOfRelease = movie24.getYearOfRelease();

         System.out.println(" Default Constructor (24th James Bond Movie)" +
         		   " after setting the values: \n \t" +
                            " James bond Movie Name: " + movieName +
                            ", Year of Release: " + movieYearOfRelease);
     }
 }



Figure: A Constructor in Java

Next, let us run the above program and see the output.

  Inside the parameterized constructor, initializing the object, Movie Name: Dr.No
  Inside the parameterized constructor, initializing the object, Movie Name: Moonraker
  Inside the parameterized constructor, initializing the object, Movie Name: Skyfall
  Inside the default constructor 
  1st James Bond Movie Name: Dr.No, Year of Release: 1962
  11th James bond Movie Name: Moonraker, Year of Release: 1979
  23rd James bond Movie Name: Skyfall, Year of Release: 2012

  Default Constructor (24th James bond movie, details not know yet) 
  	 James bond Movie Name: Jame Bond Movie, Year of Release: 0
  Default Constructor (24th James Bond Movie) after setting the values: 
  	 James bond Movie Name: Spectre, Year of Release: 2015

A constructor in Java has the same name as that of a class; notice the name "Movie" in the above example. The main purpose of a constructor is to initialize the objects, an object initialization is done as soon as they are created. A default constructor is one that takes no arguments, it initializes all the objects with the same default values. On the other hand, a parameterized constructor takes the arguments and initializes the object with the values passed (at the time of object creation) as arguments to the constructor. In the above example the default constructor does not add any value, it is added merely, so that we know the difference between a default constructor versus parameterized constructor. The above example uses the keyword "this", we will see about this key word in the next section.

Constructor overloading

Constructors follow the same semantics of a method, 2 or more methods can have the same name provided their signature changes. If you are coming from "C programming" back ground, this sounds little crazy. But, yes Java supports this concept and is called "overloading", we will see more about this in the next section understanding overloading. In the above example, we have overloaded the constructor, default constructor has zero arguments while the parameterized constructor has 2 arguments. This concept of having the same name for the constructors is called "Constructor overloading". We could overload constructor as many times as we want, constructor with 1 argument, constructor with 2 args and so on.

"this" Keyword

The main purpose of "this" key word is to reference the current object. One of its usage is shown in the above examples, notice the names of the parameters passed to the constructor and the local variables used inside, they are same. The local variables corresponds to the "current" object (the object that invoked the constructor), "this" key word references the current object and thus helps in differentiating the variable that has the same name. Notice the usage of "this" keyword the above example. We could have used different names for the constructor parameters and local variables, same names are used in the above examples to demonstrate the use of "this" keyword.





comments powered by Disqus