Join us on :

OOPS in Java

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 the 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 variable 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 a alphabets and most of the time we follow CamelCase convention, First letter of all the words are in Upper case.

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

Interfaces : Interfaces are empty containers which sets the rules for other interfaces and for classes.

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

Object : As everyone says, it is like 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 object for any class in Java, to create we will use most preferred way. i.e with new key word.

								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 which will loads all the non-static elements in the class into space that reserved by the new keyword, Constructor name is same as Class name

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

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

Constructors in Java

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

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.

Constructor has following properties:

  • Constructor has same name as class name.
  • It is used for initializing variables of class..
  • It is called whenever object of class is created..
  • It does not have return type, not even void..
  • It can have parameter.
  • Constructors can be overloaded, which means a class can have more than one constructor.
  • Overriding doesnot applicable for constructor, as the overriding occurs on inheritance with different classes..
  • A constructor may have any access modifier like private, protected, default, public..
  • Default constructor is automatically inserted when Compiler does not find any valid declaration of a constructor inside a class..
Types of Constructor:
  1. Default Constructor
  2. No-Param construtor
  3. Parameterized Constructor
  4. Copy Constructor

Default Constructor :

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

Default constructor will be created only in case of when user doesnot provide a constructor.

Why do we need 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 it is used by any code.

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

In below example there is no constructor an default constructor will be formed by the constructor, as like everyone I also think that 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();

Default Constructor in Selenium:

Selenium browser classes (FirefoxDriver, ChromeDriver) has param and no-param constructors so which 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 compiles 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();

No-Param constructor in Selenium:

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

  • new FirefoxDriver(); // chrome as well
  • new Properties()

Parameterized Constructor :

A constructor having parameter is called parameterized constructor, 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);

Parameterized Constructor in Selenium:

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

Select class and Actions class constrctors also accepts parameter 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 create 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 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. Duplicate object in the 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 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 donot have any classes which support copy-constructor

Constructor Overloading :

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

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

Benefit of Constructor overloading : Benefit of having constructor overloading is Backward/Forward Compatibility. Say after sometime you decide that 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 than 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 completely 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 callig the constructors of FirefoxDriver class, ChromeDriver class, etc.

These browser driver classes has the overloaded constructors, but most of the time what we use is FirefoxDriver, ChromeDriver, there are other constructor which accept the parameter of different type

Few of the overloaded constructor are 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 constructor with help of the this() method in java, this method always should be the first line in the Constructor.

public class K {
	public K() {
		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();

Output of constructor calling constructor

Parameterized Constructor
No param constructor			

About Author

Article is written by Pavan (a) KarthiQ. Well, I am serving notice period in an MNC, Bangalore. I thought to enrich every person knowledge a little, I always have a feeling, when we teach something, we will learn more than what you know. Knowledge is the only thing that doubles when you spend it.

I have also created the reporter for Protractor Jasmine. Use for your projects without any hesitation

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions