OOPs Concept in Selenium - Must know topics for Automation Tester

                  OOPS Concept in Selenium

As we know Automation testing has increasing demand in market. Most of the companies are utilizing automation to increase efficiency for their business process and operations. Because of which Market is showing a boom in automation sector. Automation testing provides valuable tools for business to use to their advantage, which results in improving quality and delivery standards for their customer. Many freshers and Manual testers want to kick start their career in Automation testing, but before learning about automation tools and processes one must go through basics of Java.One of the most important topics in basics of Java is OOPS CONCEPT. I will try to explain it in most simple words so that even non-tech guy can understand it.

                                    
OOPs concept in Selenium

What is OOPs?

OOPs stands for 'Object Oriented Programming System/Structure'. It is a methodology or way to simplify software development and maintainence process by providing some concepts. It has below concepts in it:
  • Classes & Objects
  • Inheritance
  • Polymorphism
  • Encapsulation
  • Abstraction
We will have a look into each of the concepts in Detail:

Classes & Objects:

Classes & Objects are introduced in order to solve real world problem through coding and programming. Classes are templates and Objects are instance of class.
  • Class is collection of object
  • Class is not real world entity. It is just a template.
  • Class doesnot occupy memory
For Objects we can say that, 
  • Object is an instance of class
  • Objects are real world entity
  • Object occupies memory
  • Object consists of : Identity, State/Attribute, Behavior
Example: Animal is Class. Different type of animals like dog, cat, horse are objects.
Animal is not a real world entity. Its just a template. Different type of animals under it are entity. As Animal is not a real world entity, so it wont occupy any memory. 

Animal can be Huskey, Bulldog,etc. That means, Object has identity.
Every Animal has color, Weight,etc. That means, it has some state/Attribute.
Every Animal can bark, run , jump,etc . That means, it has some behavior.
All these are characteristic of objects and can be implemented in programming.
An object can be create using "new" keyword.
Lets understand in code:

class Animal{
    String color;
    int weight;
    public void bark()
    {
        System.out.println("Dog is barking");
    }
    public static void main(String[] args){
        Animal bulldog = new Animal();
        bulldog.color="Black";
        bulldog.weight = 60;
        System.out.println("Color: " + bulldog.color+ " Weight: " + bulldog.weight);
        bulldog.bark();

        Animal huskey= new Animal();
        huskey.color="White";
        huskey.weight = 40;
        System.out.println("Color: " + huskey.color+ " Weight: " + huskey.weight);
        huskey.bark();
    }
}

Output: Color: Black Weight: 60
              Dog is barking
              Color: White Weight: 40
         Dog is barking

In above program, we have created 2 objects bulldog and huskey. We have printed its attributes like color and weight and also printed its behavior of barking.
This is how we can create object and classes.

Inheritance

This is one of the important concept in OOPs. Using this concept, one class can inherit features and method of sub class using extend keyword.

Example:
class Vehicle {
  protected String brand = "Maruti";
 public void drive() {
    System.out.println("I am from Parent class");
  }
}

class Car extends Vehicle {
  private String modelName = "Brezza";

  public static void main(String[] args) {
    Car myFastCar = new Car();
    myFastCar.drive();
    System.out.println(myFastCar.brand + " " + myFastCar.modelName);
  }
}

Output: I am from Parent class
              Maruti Brezza

Explanation: We have Vehicle as Parentclass. It has 'brand' as String variable and 'drive()' method.
Vehicle is child, which has 'modelName' as String variable.
We are inheriting all the properties of Parent class using "extends" keyword into child class.
We are creating object of child class by using "Car myFastCar = new Car();
Hence, by using object we can access method of Parent class i.e; drive().



Polymorphism

Polymorphism means 'many forms'. while writing program, we come accross situation where we have same method name in different classes but its implementation are different. This behahvior can be achieved using polymorphism concept.
Example: 

class Car{
  public void model() {
    System.out.println("The Car has a model");
  }
}

class Hyundai extends Car{
  public void model() {
    System.out.println("The Hyundai model is Creta");
  }
}

class Tata extends Hyundai{
  public void model() {
    System.out.println("Tata model is Harrier");
  }
}

class Main {
  public static void main(String[] args) {
    Car myCar= new Car(); //Car class object
    Car creta= new Hyundai(); //Hyundai Object
    Car harrier= new Tata(); //Tata Object
        
    myCar.model();
    creta.model();
    harrier.model();
  }
}

Output: The Car has a model
               Hyundai model is Creta
               Tata model is Harrier

Explanation: We have extended method of Car class into Hyundai class using extend keyword and similarly extended method of Hyundai class into Tata class.
We have then Created object of all the classes and accordingly called respective method.When called model using Car class object "The Car has a model" got printed.
When called a method using Hyundai class object "Hyundai model is Creta" got printed
And when called using Tata class object "Tata model is Harrier" got printed.

Polymorphism is divided into 2 types:
Run time Polymorphism also called as "Method Overloading"
Compile time Polymorphism also called as "Method Overriding"

Encapsulation

This concept is used to hide sensitive data by declaring class/variabbles as "private".
Getter and Setter methods are used to access and update value of private methods or variables.

Example:

class student
{
private int age; //private field

public int getAge()  //getter method
{
return age;
}
public void setAge(int age)  //setter method
{
this.age = age;
}
}

class Main 
{
public static void main(String args[])
{
student a= new student();   //creating objeect of student class
a.setAge(12);  //setting age to 12
System.out.println("student age is " + a.getAge());  //fetching age value using getter method
}
}

Output: student age is 12

Explanation: In this exammple, we declared private field "getAge" which can't be accessed outside the class.
To access age we used the public method "setAge".Keeping age private helped us to restrict the unautorized access from out side the class. That's why it is also called Data hiding.

Abstraction:

Data Abstraction can be defined as process of showing only essential characteristics and ignoring the irrelevant details.
  • Abstract Class and method are declared with a Abstract keyword
  • Actract method is declared without implementation
  • Abstract Class may or may not have all abstract method but it should atleast have one abstract method
  • We can' create object of an abstract class that means abstract class can't be instantiated with 'new' keyword.
Sometimes, we need methods to keep as abstract because we want other classes to do implementation as per their needs.
Example:  We have a class called Car. In this class, we have declared methods like Wheel, Steering, Price and Seats.
We have another classes like Hyundai and Tata and we need some common methods in both these classes like Wheel and Steering, however we would need methods like Price and Seats whose implementation should be different for Hyundai and Tata. (As price would vary and seats can be 5 or 7).

In this case, we can't create object of the class and inherit methods as same implementation would be shown for Hyundai and Tata, that means we can't modify the implementation.
Hence, if we declare Price and Seats method as abstract then we can Overde methods into Hyundai and Tata class by using 'override' keyword and put respective implementation. In such way the implementation can also be made diffeeent for both the classes.

Conclusion

We would be using OOPs concept in Selenium because in Automatio Framework we would be dealing with n number of Classes and in order to keep our framework structured and reusable we must know these basic Java concepts.

Comments

Popular posts from this blog

Software Testing Life Cycle - Frequently asked Interview Question

Test Engineer roles and responsibilities- Job description and Salary

Testing Techniques in Software Testing- Find bugs in seconds