Encapsulation
public class Employee {
// Private state (data)
private String name;
private int age;
// Getter method for name
public String getName() {
return name;
}
// Setter method for name
public void setName(String name) {
this.name = name;
}
// Getter method for age
public int getAge() {
return age;
}
// Setter method for age
public void setAge(int age) {
this.age = age;
}
// Main Class
public class Main {
public static void main(String[] args) {
Employee emp = new Employee();
// Setting values using setter methods
emp.setName("John");
emp.setAge(30);
// Accessing values using getter methods
System.out.println("Employee Name: " + emp.getName());
System.out.println("Employee Age: " + emp.getAge());
}
}
Encapsulation
public class LoginPage {
@FindBy(id = "username")
private WebElement usernameField;
@FindBy(name = "password")
private WebElement passwordField;
@FindBy(xpath = "//button[@type='submit']")
private WebElement loginButton;
public LoginPage(WebDriver driver) { // Constructor to
initialize web elements
PageFactory.initElements(driver, this);
}
public void clickLogin(String username, String password) {
usernameField.sendKeys(username);
passwordField.sendKeys(password);
loginButton.click();
}
}
public class LoginTest {
WebDriver driver;
LoginPage loginPage;
@Test
public void testLogin() {
loginPage = new LoginPage(driver);
loginPage.clickLogin("username", "password");
}
}
Inheritance -:
public class BaseClass {
WebDriver driver;
public void setup() {
// Browser setup code
}
public void teardown() {
// Close browser
}
}
public class TestClass extends BaseClass {
@Test
public void testExample() {
setup();
teardown();
}
}
Compile-Time Polymorphism (Static Binding): Example (Method Overloading):
class Calculator {
// Method with two parameters
int add(int a, int b) {
return a + b;
}
// Overloaded method with three parameters
int add(int a, int b, int c) {
return a + b + c;
}
}
public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println(calc.add(5, 10));
System.out.println(calc.add(5, 10, 15));
}
}
Compile-Time Polymorphism in Selenium
public void waitForElement(WebElement element, int seconds) {
// Explicit wait code
}
public void waitForElement(String locator, int seconds) {
// Explicit wait with a string locator
}
Run-Time Polymorphism (Dynamic Binding): Example (Method Overriding):
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Animal animal = new Dog(); // Polymorphic behavior
animal.sound(); // Calls Dog's implementation at
runtime
}
}
Using Selenium -: Run-Time Polymorphism
The WebDriver interface allows runtime flexibility to execute tests
across multiple browsers.
WebDriver driver = new ChromeDriver(); // Chrome
driver.get("https://example.com");
driver = new FirefoxDriver(); // Firefox
driver.get("https://example.com");
Abstraction-: Abstract Class:
abstract class Vehicle {
abstract void start(); // Abstract method
void stop() { // Concrete method
System.out.println("Vehicle stopped");
}
}
class Car extends Vehicle {
@Override
void start() {
System.out.println("Car started");
}
}
Interface:
interface Vehicle {
void start();
void stop();
}
class Car implements Vehicle {
@Override
public void start() {
System.out.println("Car started");
}
@Override
public void stop() {
System.out.println("Car stopped"); }}