Selenium WebDriver: Constructors
What Is Constructor In Java?
Constructor in java is a special type of method that is used to initialize the object.
Java constructor is invoked at the time of object creation. It constructs the values i.e. provides data for the object that is why it is known as constructor.
Rules for creating java constructor
There are basically two rules defined for the constructor.
- Constructor name must be same as its class name
- Constructor must have no explicit return type
Types of java constructors
There are two types of constructors:
- Default constructor (no-arg constructor)
- Parameterized constructor
Java parameterized constructor
A constructor that have parameters is known as parameterized constructor.
Why use parameterized constructor?
Parameterized constructor is used to provide different values to the distinct objects.
Example of parameterized constructor
In this example, we have created the constructor of Student class that have two parameters. We can have any number of parameters in the constructor.
class Student4{
int id;
String name;
Student4(int i,String n){
id = i;
name = n;
}
void display(){System.out.println(id+" "+name);}
public static void main(String args[]){
Student4 s1 = new Student4(111,"Karan");
Student4 s2 = new Student4(222,"Aryan");
s1.display();
s2.display();
}
}
Output:
111 Karan
222 Aryan
Constructor Overloading in Java
Constructor overloading is a technique in Java in which a class can have any number of constructors that differ in parameter lists.The compiler differentiates these constructors by taking into account the number of parameters in the list and their type.
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
Difference between constructor and method in java
There are many differences between constructors and methods. They are given below.
Java Constructor
- Constructor is used to initialize the state of an object.
- Constructor must not have return type.
- Constructor is invoked implicitly.
- The java compiler provides a default constructor if you don't have any constructor.
- Constructor name must be same as the class name.
Java Method
- Method is used to expose behaviour of an object.
- Method must have return type.
- Method is invoked explicitly.
- Method is not provided by compiler in any case.
- Method name may or may not be same as class name.
No comments:
Post a Comment