A class is a blueprint. An object is a thing built from that blueprint. In automation, LoginPage is a class — it defines what a login page can do (enter username, click submit). When you write LoginPage login = new LoginPage(driver), you create an object of that class.
public class BankAccount {
// Fields (state)
String accountHolder;
double balance;
String accountType;
// Method (behavior)
void deposit(double amount) {
balance += amount;
System.out.println("Deposited: " + amount + " | Balance: " + balance);
}
void withdraw(double amount) {
if (amount > balance) {
System.out.println("Insufficient funds");
return;
}
balance -= amount;
System.out.println("Withdrawn: " + amount + " | Balance: " + balance);
}
void printDetails() {
System.out.println(accountHolder + " | " + accountType + " | Balance: " + balance);
}
}public class BankTest {
public static void main(String[] args) {
// Create objects from the class
BankAccount acc1 = new BankAccount();
acc1.accountHolder = "John";
acc1.accountType = "Savings";
acc1.balance = 1000;
BankAccount acc2 = new BankAccount();
acc2.accountHolder = "Jane";
acc2.accountType = "Current";
acc2.balance = 5000;
acc1.deposit(500);
acc1.withdraw(200);
acc1.printDetails();
acc2.withdraw(10000);
acc2.printDetails();
}
}Deposited: 500.0 | Balance: 1500.0
Withdrawn: 200.0 | Balance: 1300.0
John | Savings | Balance: 1300.0
Insufficient funds
Jane | Current | Balance: 5000.0| OOP Concept | Automation Equivalent | Example |
|---|---|---|
| Class | Page Object | LoginPage, DashboardPage |
| Fields | Element locators | By.id("username"), By.css(".submit") |
| Methods | Page actions | login(), getTitle(), isErrorVisible() |
| Object | Instance for a test | new LoginPage(driver) |
this refers to the current object. Use it when a parameter has the same name as a field, or when returning the current object for method chaining.
public class User {
String name;
String email;
void setDetails(String name, String email) {
this.name = name; // this.name = field, name = parameter
this.email = email;
}
void print() {
System.out.println(this.name + " — " + this.email);
}
}Q: What is the difference between a class and an object?
A: A class is a template/blueprint that defines fields (data) and methods (behavior). An object is a specific instance of a class created with the new keyword. You can create multiple objects from one class, each with its own data. Example: String is a class, "hello" is an object of that class.
Exercise 1: Create a TestCase class with fields: id, name, status, executionTime. Add a method printSummary() that formats and prints all fields. Create 3 TestCase objects and call printSummary on each.
Exercise 2: Create a Calculator class with methods add, subtract, multiply, divide. Each method takes two parameters and returns the result. Use it from main.