Part 1: OOPs concepts in Selenium-Java Automation Framework

Sandeep
7 min readJan 22, 2023
Photo by Jelleke Vanooteghem on Unsplash

As we all are aware a computer program consists mainly of two things code and data. A program can be written around its code or around its data. The program that is written in such a way that code acts on data is called a Process-oriented model and the one which is written to act around its data is called Object-oriented programming(OOPs). An object-oriented program can be characterized as ‘data controlling access to code’.

The main intention of this post is to give a brief about the concepts of OOPs being used in a Selenium automation framework. This post is divided into two parts:

  • OOPs concepts refresher(Java)
  • OOPs concepts used in the Selenium-Java automation framework

I. OOPs concepts refresher(Java)

Object-Oriented programming has basically four pillars which are below:

  • Abstraction
  • Encapsulation
  • Inheritance
  • Polymorphism

i.Abstraction

Abstraction is a process of hiding implementation details and the user does not have to know what is happening behind the scenes when the user is consuming the functions. For example, when you turn ON the laptop you press the power button of your laptop and once the display is ON (that's it!!) you don’t care about the inner mechanism that went and turned ON the laptop. Please find a sample Java code for the same below:

//Abstract class
public abstract class Car {
public abstract void start();
public abstract void accelerate();
public abstract void stop();
}

//Normal class
public class Tata extends Car {
@Override
public void start() {
System.out.println("Tata start method");
}

@Override
public void accelerate() {
System.out.println("Tata accelerate method");
}

@Override
public void stop() {
System.out.println("Tata stop method");
}
}

//Main class
public class abstractClass {
public static void main(String args[])
{
Car object = new Tata();
object.stop();
}
}

Please find the explanation for the above code below:

  • Car is an abstract class which consists of the start(), stop() and accelerate() methods as you can see there is no implementation
  • Now Tata is inheriting Car and there is implementing the method
  • And in the abstractClass which is our main class there, we are calling the methods

ii. Encapsulation

Encapsulation is the mechanism that binds together code and data it manipulates. Technically Data Encapsulation can be defined as wrapping the code or methods (properties) and the related fields or variables together as a single unit. In object-oriented programming, we call this single unit — a class, interface, etc. We can consider it as a capsule(as the name suggests) which consists of a mix of several medicines. In our case(Java) its methods and variables.

Courtesy of https://www.softwaretestingo.com/

Encapsulation in java can be achieved by the following:

  • By defining the variables of a class as private
  • Using getter and setter methods to modify and read variable values

Please find a sample program of encapsulation below:

/* File name - StudentDetails */
public class StudentDetails {
private String studentName;
private String studentAge;
private int studentId;

public int getAge() {
return studentAge;
}

public String getStudentName() {
return studentName;
}

public String getStudentId() {
return studentId;
}

public void setStudentAge( int inputStudentAge) {
studentAge = inputStudentAge;
}

public void setStudentName(String inputStudentName) {
studentName = inputStudentName;
}

public void setStudentId( String inputStudentId) {
studentId = inputStudentId;
}
}

/* File name - EncapsulationExample */
public class EncapsulationExample{

public static void main(String args[]) {
StudentDetails studentDetails = new StudentDetails();
studentDetails.setStudentName("James Gosling");
studentDetails.setStudentAge(18);
studentDetails.setStudentId("java_lead_designer");

System.out.print("Name : " + encap.getName() + " Age : " + encap.getAge());
}
}

Please see the explanation for the above code created for a student below:

  • We have created a class called StudentDetails and we have defined the variables as private so they can’t be modified outside of the class.
  • Within the class also the getter and setter methods can only read and modify the values. For example, to get studentAge we will have to use getAge() method and to set the studentAge we will have to use setStudentAge()

iii. Inheritance

Inheritance is one of the most important concepts of OOPs. Inheritance is the process by which one object acquires the properties of another object. If we want to understand it from the real world perspective we can take the example of the vehicle where a Car, Bus and Bike all come under the category called Vehicle. Inheritance represents an IS-A relationship which is also known as a parent-child relationship.

The main reason why we use inheritance is below:

  • Method overriding which we will see in the explanation of Polymorphism
  • Code reusability

Terms used in Inheritance are below:

  • Class: A class is a collection of objects which have common properties.
  • Derived class/Sub-class: Derived class is a class that inherits from a base class. It is also known as subclass or child class.
  • Base Class/Superclass: The base class is the main class where a derived class inherits the feature. It is also known as the superclass or parent class.

Types of inheritance are below:

a. Single Inheritance: When a single class inherits another class it's called Single Inheritance.

Single inheritance. Courtesy of programiz.com.
  • Sample code for single inheritance :
//Parent class
public class Parent{
public void display() {
System.out.println("I am a method from Parent class");
}
}

// Child is inheriting display method of Parent
class Child extends Parent{
public void print() {
System.out.println("I am a method from Child class");
}

public static void main(String[] args) {
Child objChild = new Child();
objChild.display(); // Reusing the Parent named "display"
objChild.print();
}
}

b. Multilevel inheritance: Whenever there is a chain of inheritance it's called multilevel inheritance.

Multilevel inheritance. Courtesy of programiz.com.
  • Sample code for multilevel inheritance :
class Shape {
public void display() {
System.out.println("Inside display method of Shape class");
}
}
class Rectangle extends Shape {
public void area() {
System.out.println("Inside area method of Rectangle class");
}
}
class Cube extends Rectangle {
public void volume() {
System.out.println("Inside volume method of Cube class");
}
}
public class MultilevelInheritanceClass {
public static void main(String[] arguments) {
Cube cube = new Cube();
cube.display();
cube.area();
cube.volume();
}
}

c. Hierarchical inheritance: When multiple different classes inherit a single class it's called Hierarchial inheritance.

Hierarchical inheritance. Courtesy of programiz.com.
  • Sample code for Hierarchical inheritance :
public class Parent{
public void display() {
System.out.println("I am a method from Parent class");
}
}

public class FirstChild extends Parent{
public void firstChildMethod() {
System.out.println("I am a method from FirstChild class");
}
}
public class SecondChild extends Parent{
public void secondChildMethod() {
System.out.println("I am a method from SecondChild class");
}
}

class MainClass{
public void print() {
System.out.println("I am a method from MainClass class");
}

public static void main(String[] args) {
FirstChild firstObjectChild = new FirstChild();
SecondChild secondObjectChild = new SecondChild();
firstObjectChild.firstChildMethod();
secondObjectChild.secondChildMethod();
}
}

d. Multiple inheritance: When a single class inherits from multiple classes it's called as multiple inheritance. Please take note that Multiple inheritance is not supported in Java.

Multiple inheritance. Courtesy of programiz.com.

e. Hybrid inheritance: Hybrid inheritance is a combination of more than one type of inheritance.

Hybrid inheritance. Courtesy of programiz.com.

iv. Polymorphism

Polymorphism is an important concept of object-oriented programming. It simply means more than one form. Java supports two types of polymorphism which are below:

  • Compile-time polymorphism(static polymorphism)
  • Runtime polymorphism(dynamic polymorphism)

a. Compile-time polymorphism

This type of polymorphism is achieved by creating multiple methods with the same name in the same class, but with different numbers of input parameters or parameters of different data types. This is called method overloading. The method which needs to be picked up is decided by the compiler hence the name compile-time polymorphism. Please find a sample code below:

class Addition {
// Method 1
static void add(int a, int b)
{
System.out.println("Integer Addition and Result = "+ a*b);
}

// Method 2
static void add(double a, double b)
{
System.out.println("Double Addition and Result = "+ a*b);
}

// Method 3
static void add(double a, double b, double c)
{
System.out.println("Three parameters, Double Addition, and Result = "+ a*b);
}
}

public class Main {
public static void main(String[] args) {

//Using the First method
Addition.add(3,5);

//Using the Second method
Addition.add(3.5,5.1);

//Using the Third method
Addition.add(3.6,5.2, 6.3);
}
}

b. Run-time Polymorphism

If the same method is present in the child class and the parent class then the method in the child class overrides the parent class. This is called Method Overriding. The method which needs to be called is determined during the execution of the program hence Run-time Polymorphism. Please find the sample code below:

class ParentClass{
public void displayInfo() {
System.out.println("I am from ParentClass");
}
}

class ChildClass extends ParentClass {
@Override
public void displayInfo() {
System.out.println("I am from ChildClass");
}
}

class Main {
public static void main(String[] args) {

// Create an Object of ChildClass
ChildClass objFirst= new ChildClass();

objFirst.displayInfo();
// Create an Object of ParentClass
ParentClass objSecond= new ParentClass();
objSecond.displayInfo();
}
}

That’s All Folks. I will continue with this topic in Part 2 of this article. Happy coding 😃!!

All References:

  1. https://www.javatpoint.com/
  2. https://www.tutorialspoint.com/
  3. https://www.programiz.com/

--

--