Selenium/Java- CoC
From last post, we started to go through some of the basic concepts of Java which are needed to embark our journey of Selenium. On the same line, let us learn one such very important concept that is nothing but CoC (Concept of Constructor..!).
In general, constructor is a code that is used to initialize objects of the class,for more info on class and object, please visit Selenium with JAVA… . Specifically, it is one special type of method to pass the value to the object at the time of object creation. Some people say that method and constructor is same in Java but that is not the fact due to following reasons. Hence, I call constructor as one special type of method.
- Constructor must have same name as of the class which is not the case for methods..!
- Constructor does not have any return type but method surely does..!
- Constructor can not be abstract, final or static but method can..!(Relax, we will learn about all the three keywords in coming posts..!!)
There are broadly three types of constructors.
- Default Constructor: When you don't insert any constructor in your code, Java does that for you. It would be inserted into code during compilation time but if you insert any other constructor, Java will not bother to implement default constructor for you..!
If you have below code in your .Java file without any constructor,
public class Bank {
public static void main(String args[]){ // main method
Bank SBI = new Bank(); // object creation syntax
}
}
At compile time, it will look like below in .class file.
public class Bank {
Bank(){ // Java inserts default constructor for you !!
}
public static void main(String args[]){ // main method
Bank SBI = new Bank(); // object creation syntax
}
}
2. No-argument Constructor: As the name suggests, it is a constructor with no arguments. Its signature is same as default constructor but it can contain any code unlike the default one.
public class Bank {
Bank(){ // No-arg constructor can contain any code statements.
System.out.println("No money in account??, Start Investing :P");
}
public static void main(String args[]){ // main method
Bank SBI = new Bank(); // object creation syntax
}
}
3. Parameterized Constructor: Here, we pass the values in the constructor and initialize objects with these values at the time of object creation.
public class Bank {
Bank(int accountNumber, String customerName){ // Parameterized constructor
this.accountNumber=accountNumber; // 'this' is a keyword used to refer to the current object and differentiate instance variable from local variable.
this.customerName=customerName;
}
void info(){
System.out.println(“Account Number is: “+accountNumber+” Customer Name is: “+customerName);
}
public static void main(String args[]){ // main method
Bank SBI = new Bank(123,"Raj"); // object creation syntax, we are initializing our objects here
Bank HSBC=new Bank(456,"John");
SBI.info();
HSBC.info();
}
}
That's all about our CoC for now, code these things in eclipse and try different manipulations to fetch the best possible knowledge by your self. It will help a lot.
See you soon with more posts on Java and Selenium...!
Cheers..!!
Nice Broππππ
ReplyDeleteThank you :) keep up the learning !!
ReplyDelete[…] last post, we learnt about different types of constructors Selenium/Java- CoC in Java which are important and very useful while writing Selenium automation scripts. Now we […]
ReplyDelete