OOPs concepts in Java – Mastering Selenium with Inheritance

Selenium leverages Object-Oriented Programming (OOP) principles to build scalable and maintainable automation frameworks. Core OOP concepts—Inheritance, Polymorphism, and Encapsulation—play a key role in designing robust test architectures.

In this article, we will focus on how Inheritance is used in Selenium with Java to promote code reusability, reduce duplication, and create a structured automation framework. If you’d like to explore Polymorphism in detail, refer to: OOP Concepts in Java – Mastering Selenium with Polymorphism.

Inheritance in Object-Oriented Programming is mainly categorized into Single Inheritance, Multilevel Inheritance, and Multiple Inheritance (which is not directly supported in Java using classes but can be achieved through interfaces).

Inheritance enables a child class to acquire the properties and behaviors (methods) of a parent class, promoting code reusability and maintainability. In Java, a parent class shares its variables and methods with a child class using the extends keyword, allowing the child class to reuse and enhance existing functionality.

Single Inheritance:

In Single Inheritance, a parent class shares its properties and behaviors with a single child class. This allows the child class to reuse the methods and variables defined in the parent class, reducing code duplication.

In the example below, the Animal class acts as the parent class, and it shares its methods with the child class Dog, which extends the functionality of the parent.

Example code:

Animal Class

class Animal {
public void eat() {
// code goes here
}

public void smell() {
// code goes here
}

}

Dog Class-

class Dog extends Animal {
public void bark() {
// code goes here
}
}

Multi Level Inheritance:

In Multilevel Inheritance, properties and methods are passed through a chain of classes, allowing multiple levels of inheritance.

In the example below, Animal is the parent class of Mammal, and Mammal acts as the parent class for Dog. With this structure, the properties and methods of the Animal class are inherited by both Mammal and Dog. Additionally, the Dog class also inherits the properties and methods of the Mammal class, enabling a layered and reusable design.

Example Code:

Animal Class-

class Animal {
public void eat() {
// code goes here
}

public void smell() {
// code goes here
}

}

Mammal Class-

class Mammal extends Animal {
public void feeding() {
// code goes here
}
}

Dog Class-

class Dog extends Mammal {
public void bark() {
// code goes here
}
}

Multiple Inheritance:

Multiple Inheritance is not supported in Java through classes due to the diamond problem. This issue occurs when a child class tries to inherit from two parent classes that contain the same methods or properties, leading to ambiguity about which implementation should be used.

To avoid this confusion, Java does not allow multiple inheritance with classes. However, similar behavior can be achieved using interfaces, where a class can implement multiple interfaces without ambiguity in implementation.

Selenium Implementation with Inheritance:

In the following example, the WebDriver setup is defined in the Base class within the setUpDriver() method and executed using the @BeforeSuite annotation. This setup is then inherited by the subclass Class1 using the extends keyword.

By doing this, the WebDriver initialization is centralized in the parent class, allowing the child class to reuse the same driver instance and execute tests without duplicating setup code.

Example code:

Base Class-

package tests;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeSuite;

public class BaseClass {
WebDriver driver = null;
@BeforeSuite
public WebDriver setUpDriver() {
driver = new ChromeDriver();
return driver;
}
}

Class1 Test class-

package tests;

import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;

public class Class1 extends BaseClass{

@Test
public void Test1() {
driver.get(“https://www.google.com”);
}

@AfterTest
public void CleanUp() {
if(driver != null) {
driver.quit();
}
}
}

For YouTube Video Click On: OOP Concepts in Java | Master Inheritance in Selenium Framework

0 0 votes
Article Rating
Subscribe
Notify of
guest

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

[…] In this article, we will focus on how Polymorphism is applied within Selenium automation frameworks. If you’d like to explore Inheritance in detail, refer to: OOP Concepts in Java – Mastering Selenium with Inheritance. […]

Subscribe to our YouTube Channel: Testing Talks Latest
Testingtalkslatest.com - A project by CreativeHub IT Solutions.
Contact Us At: support@testingtalkslatest.com
Our Partner websites - Classified Hub , CodesToolbox
Scroll to Top
1
0
Would love your thoughts, please comment.x
()
x