Selenium WebDriver Java Tutorials : Constructors

What Is Constructor In Java software development?
If interviewer asks you a definition of constructor then you can give him answer like : Constructor Is a code block which Is Called and executed at the time of object creation and constructs the values (i.e. data) for object and that's why It Is known as constructor. Each class have default no argument constructor In Java software development language If you have not specified any explicit constructor for that class. In java software development language, Constructor looks like methods but bellow given properties of constructor are differentiate them from the methods. You can VIEW TUTORIALS OF METHODS IN JAVA.

Constructor Creation Rules
  • Name of the constructor must be same as class name in java software development language. Means If your class name Is Student then constructor name must be Student.
  • Constructor must have not any return types.
Same as methods, We can pass parameters to constructors. Let me show you simple example of parameterized constructor In java software development language. You can also view my PREVIOUS POST to see the example of constructor.

public class Student {
       public static void main(String[] args) {
            //Two different objects created with value.
            Student stdn1 = new Student("Michael"); 
            Student stdn2 = new Student("Robert");
      }
 
      //Constructor with parameter to pass values of object
      //Name of constructor Is same as class name.
      public Student(String name){ 
            String stdnname = name;
            System.out.println("Student's Name Is "+stdnname);
      }

}

Console output will looks like bellow.
Student's Name Is Michael
Student's Name Is Robert

In above example, Constructor will be called at the time of objects creation. Constructor will pass the values of objects one by to print them.

Constructor Overloading
As we know, Constructors name must be same as class name, When you create more than one constructors with same name but different parameters In same class Is called constructor overloading. So question Is why constructor overloading Is required in java software development language? Constructor overloading Is useful when you wants to construct your object In different way or we can say using different number of parameters. You will understand It better when we will look at simple example but before that let me tell some basic rules of constructor overloading.
  • Two constructors with same arguments are not allowed for constructor overloading.
  • You need to use this() keyword to call overloaded constructor.
  • If you are calling constructor from overloaded constructor using this() keyword, It must be first statement.
  • It is best practice to call constructor from overloaded constructor to make It easy to maintain.
Bellow given example shows best example of constructor overloading.
public class Student {
 String finame;//Instance variable
 String miname;//Instance Variable 
 public static void main(String[] args) {
  Student stdn1 = new Student("Jim");
  Student stdn2 = new Student("Mary", "Elizabeth");
 }
 
 //Constructor with one argument.
 public Student(String fname){ 
  finame = fname;//Local Variable  
  System.out.println("1. First Name Is "+finame);
 }
 
 //Overloaded Constructor with two arguments.
 public Student(String fname, String mname){
  finame = fname;
  miname = mname;
  System.out.println("2. First Name Is "+finame);
  System.out.println("2. Middle Name Is "+miname);  
 } 
}

Console output will looks like bellow.
1. First Name Is Jim
2. First Name Is Mary
2. Middle Name Is Elizabeth

We can do same thing using single object creation too using this() keyword as bellow.
public class Student {
 String finame;
 String miname; 
 public static void main(String[] args) {
  Student stdn2 = new Student("Mary", "Elizabeth");
 }
 
 //Constructor with one argument.
 public Student(String fname){ 
  finame = fname;  
  System.out.println("1. First Name Is "+finame);
 }
 
 //Overloaded Constructor with two arguments.
 public Student(String fname, String mname){
  this("Jim"); //1st constructor Is called using this keyword.
  finame = fname;
  miname = mname;
  System.out.println("2. First Name Is "+finame);
  System.out.println("2. Middle Name Is "+miname);  
 } 
}
If you will run above example then console's output will be same but you can see we have created only one object Inside main method. Used this keyword In overloaded constructor to call another constructor.

4 comments:

  1. Is there any major difference between constructor overloading and method overloading other than that constructor uses this keyword and has same name as classname whereas method has a different name.

    ReplyDelete
    Replies
    1. You can have different methods which are having same signature i.e. method overriding but you cannot have constructor overriding i.e 2 constructor with same number and type of argumenets

      Delete
  2. 1.Constructor do not Support return type as Methods do,
    2.and to access constructor we have to create object each time

    ReplyDelete
  3. Thank you for the explanation. was able to implement it perfectly

    ReplyDelete