OOPS in Selenium

Object means a real-world entity such as a pen, chair, table, etc. Object-Oriented Programming is a methodology or paradigm to design a program using classes and objects.

It simplifies software development and maintenance by providing some concepts:

  1. Object
  2. Class
  3. Inheritance
  4. Polymorphism
  5. Abstraction
  6. Encapsulation

Classes in Java

A class is a user-defined blueprint or prototype from which objects are created. It represents the set of variables and methods that are common to all objects of one type.

Modifiers : A class can be public or has default access

Class name : The name should begin with alphabets, and most of the time, we follow the CamelCase convention; the first letter of all the words is in the Upper case.

Superclass : Like everyone in the real world, even Java classes can have parents, these parents are called as Superclass. Any given Class can have only one class as Parent. ( Don't ask whether it is the mother or father)

Interfaces : Interfaces are empty containers that set the rules for other interfaces and classes.

Body : A class body is contained within curly braces { }, the class can contain methods, variable, inner classes, constructors, static blocks, default blocks

Object : As everyone says, it is like a real object in our world, But Objects are nothing but the container of the non-static elements in the class, Objects will have value, address

Object Creation

We can create an object for any class in Java; to create, we will use the most preferred way. i.e., with a new keyword.

Type variable = new constructor();
A objA = new A();


Type is nothing but the type of the object.

variable is nothing but any name for the object

new is the keyword to reserve the memory space on the stack

Constructor is the one who will load all the non-static elements in the class into space that reserved by the new keyword; the Constructor name is the same as the Class name.

We can access all non-static members of the class via object reference with the dot operator.

public class A {
	public void test() {
		System.out.println("test method");
	}
	public static void main(String[] args) {
		A objA = new A();
		objA.test();
	}
}

Constructors in Java

A constructor is a block of code that allows you to create an object of the class. This can also be called creating an instance.

The constructor looks like a method, but it’s not; for example, methods can have any return type or no return type, but constructors don’t have any return type not even void.

The constructor has the following properties:

  • The constructor has the same name as the class name.
  • It is used for initializing variables of the class.
  • It is called whenever an object of the class is created.
  • It does not have a return type, not even void.
  • It can have a parameter.
  • Constructors can be overloaded, which means a class can have more than one constructor.
  • Overriding does not apply for the constructor, as the overriding occurs on inheritance with different classes.
  • A constructor may have any access modifier like private, protected, default, public.
  • A default constructor is automatically inserted when the Compiler does not find any valid declaration of a constructor inside a class.

Types of Constructor:

  1. Default Constructor
  2. No-Param constructor
  3. Parameterized Constructor
  4. Copy Constructor

Default Constructor :

Default Constructors will not accept any parameters; default constructor is created during the compile time.

The default constructor will be created only in case when the user does not provide a constructor.

Why do we need a default constructor ? At the beginning of an object's life, the Java virtual machine (JVM) allocates memory on the heap to accommodate the object's instance variables.

When that memory is first allocated, however, the data it contains is unpredictable. If the memory were used as is, the behavior of the object would also be unpredictable.

To guard against such a scenario, Java makes certain that memory is initialized, at least to predictable default values before any code uses it.

The default constructor assigns the default values to the non-static elements present in the class

In the below example, there is no constructor. A default constructor will be formed by the constructor, as like everyone I also think that the default constructor may look like No-Param constructor.

public class A {
	public void test() {
		System.out.println("test method");
	}
	public static void main(String[] args) {
		A objA = new A();
		objA.test();
	}
}

Default Constructor in Selenium:

Selenium browser classes (FirefoxDriver, ChromeDriver) have param and no-param constructors so this makes that there is no default constructor present in selenium.

No-Param constructor :

No-param constructor is nothing but a constructor without any parameters; Once a constructor is present in the class, then the compiler will not create a constructor.

public class A {
	// no param constructor
	public A() {
		// constructor block
	}
	public void test() {
		System.out.println("test method");
	}
	public static void main(String[] args) {
		A objA = new A();
		objA.test();
	}
}

No-Param constructor in Selenium:

Selenium has few no-Param constructors in its package, below are the few no-param constructors present in selenium.

    <li">new FirefoxDriver(); // chrome as well <li">new Properties()

Parameterized Constructor :

A constructor having parameter is called the parameterized constructor, a parameterized constructor can have any number of parameters and any type of parameters.

public class B {
	// no param constructor
	public B(int i, boolean flag) {
		System.out.println("Parameterized constructor");
	}
	public void test() {
		System.out.println("test method");
	}
	public static void main(String[] args) {
		B objA = new B(10, false);
		objA.test();
	}
}

Parameterized Constructor in Selenium:

Browser Classes in selenium accepts the .exe file location of the browser when the browser is not installed in the standard path, these will accept the file path or the Binary path as a parameter, and these are an example for parameterized constructors.

Select class and Actions class constructors also accept parameters in the form of Webelement and WebDriver, respectively.

  • new FirefoxDriver(Capabilities desiredCapabilities)
  • new ChromeDriver(ChromeOptions options)
  • new Select(WebElement element)
  • new Actions(WebDriver driver)

Copy Constructor :

As the name suggests, it creates a copy of the constructor; A copy constructor is a constructor that takes only one argument which is of the type as the class in which the copy constructor is implemented.

For example, let us assume a class, namely Animal, and it has a constructor called a copy constructor which accepts only one argument and that too type of Animal.

Copy constructors are widely used for creating duplicates of objects known as cloned objects. The duplicate object in a sense the object will have the same characteristics of the original object from which duplicate object is created.

But we have to ensure that both original and duplicate objects refer to different memory locations.

So we are free to use copy constructors instead of clone method in java, but recommended is a clone method.

class Foo {
	public int x;
	public int y;

	// Sample constructor
	Foo(int x, int y) {
		this.x = x;
		this.y = y;
	}
	// Copy constructor
	Foo(Foo foo) {
		this.x = foo.x;
		this.y = foo.y;
	}
}
public class AD {
	public static void main(String[] args) {
		Foo fooA = new Foo(3, 5);   // initialize foo1
		Foo fooB = new Foo(fooA);  // copy foo1 to foo2
		fooB.x = 7;
		fooB.y = 8;
		System.out.println(fooA.x + " " + fooA.y); // prints "3 5"
		System.out.println(fooB.x + " " + fooB.y); // prints "7 8"
	}
}

Copy Constructor in Selenium :

Selenium doesn't have any classes which support copy-constructor

Constructor Overloading :

Like methods, a constructor can also be overloaded. Overloaded constructors are differentiated based on their type of parameters or number of parameters.

Constructor overloading is not much different than method overloading. In the case of method overloading, you have multiple methods with the same name but different signature, whereas, in Constructor overloading, you will have multiple constructors with a different signature.

The benefit of Constructor overloading : The benefit of having constructor overloading is Backward/Forward Compatibility. Say after some time you decide that the Employee class is going to have one more property "age", along with the existing name.

Now what you do, If you change the parameterized constructor, then you will have to change at all the places where you have used it.

So to make sure that your class is adapting to the new changes while keeping the old standards intact, you create a new parameterized constructor with 2 parameters.

So now you have two constructors with an entirely different and valid purpose.

class Employee {
	public Employee(String empName) {
		System.out.println("constructor with name");
	}
	public Employee(String empName, int age) {
		System.out.println("constructor with name and age");
	}
}
public class EmployeeTest {
	public static void main(String[] args) {
		Employee emp = new Employee("karthiQ");
		Employee emp1 = new Employee("Tom", 28);
	}
}

Constructor overloading in Selenium

We have constructor overloading in selenium as well. In selenium the when we open a browser we would be calling the constructors of FirefoxDriver class, ChromeDriver class, etc.

These browser driver classes have overloaded constructors, but most of the time what we use is FirefoxDriver, ChromeDriver; there are other constructors that accept the parameter of a different type.

Few of the overloaded constructor as below : 1. Browser driver constructors :

// firefox
new FirefoxDriver()
new FirefoxDriver(Capabilities desiredCapabilities)
new FirefoxDriver(FirefoxOptions options)
// chrome
new ChromeDriver()
new ChromeDriver(Capabilities desiredCapabilities)
new ChromeDriver(ChromeOptions options)


2. Actions class constructors

new Actions(Keyboard keyboard)
new Actions(WebDriver driver)

Constructor Calling Constructor

We can call one Constructor from other constructors with the help of the this() method in java; this method always should be the first line in the Constructor.

public class K {
	public K() {
		this(10);
		System.out.println("No param constructor");
	}
	public K(int i) {
		System.out.println("Parameterized Constructor");
	}

	public static void main(String[] args) {
		K objK = new K();
	}
}

The output of constructor calling constructor

Parameterized Constructor
No param constructor
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions