Notes
Class and object
A class is a blueprint. It says what data an object has, called fields, and what it can do, called methods. An object is one thing built from the blueprint with the keyword new. Here the Product class has a sku and a price, and a constructor, the special method that runs when the object is created and sets its fields. new Product, MG two oh one, fifteen hundred cents, gives you one green mug object.
public class Product {
final String sku;
final long priceCents;
Product(String sku, long p) {
this.sku = sku;
this.priceCents = p;
}
}
var mug = new Product("MG-201",
1500); Records
For plain data, modern Java has records. One line declares the fields, and Java generates the constructor, the getters, equals, hash code and a readable to string. Records are immutable, so a test cannot change them by accident. Two records with the same values are equal, which makes assertions easy. Use records for test data: products, users, expected cart totals.
record Product(String sku,
long priceCents) {}
var mug = new Product("MG-201",
1500);
mug.sku() // "MG-201"
mug.equals(new Product(
"MG-201", 1500)) // true
constructor, getters, equals,
toString: all generated Encapsulation
Encapsulation means keeping data private and exposing only methods. A page object is the classic example. The cart page keeps its locators private: the coupon field's selector lives in one place. Tests call apply coupon and total, and never see a selector. When the page changes, you fix one class instead of fifty tests. In Part five you will build page objects for ShopRung with Playwright and Selenium; this is the idea behind them.
public class CartPage {
private final Page page;
private final String coupon =
"#coupon";
public void applyCoupon(
String code) {
page.fill(coupon, code);
page.click("text=Apply");
}
public String total() {...}
} Inheritance
Inheritance lets a child class reuse a parent class with the word extends. Many frameworks have a base test class that sets things up before every test: here it creates an API client and resets ShopRung's data. Cart test extends base test, so every cart test starts with a ready client and clean data, without repeating that code. Protected means the child can use the field. Keep inheritance shallow, one or two levels, or nobody can follow it.
abstract class BaseTest {
protected Api api;
@BeforeEach void setUp() {
api = new Api(BASE_URL);
api.post("/api/test/reset");
}
}
class CartTest extends BaseTest {
@Test void addsTenMugs() {
// api is ready, data clean
} Polymorphism
Polymorphism means many forms: code written against an interface works with any class that implements it. Selenium's WebDriver is an interface. Chrome driver and Firefox driver implement it. Your test calls driver dot get, and does not care which browser is behind it, so you choose the browser once, from a setting, and the same tests run everywhere. Method overriding, a child class replacing a parent's method, is the other side of the same idea.
WebDriver driver = switch (browser) {
case "chrome" ->
new ChromeDriver();
case "firefox" ->
new FirefoxDriver();
default -> throw new
IllegalArgumentException();
};
driver.get(SHOP_URL);
// same call, any browser Abstraction
Abstraction shows what something does and hides how. An interface declares methods without bodies: a coupon rule must say whether it applies to a subtotal. A class implements it: min subtotal applies when the subtotal is at least the minimum, so five thousand applies to a minimum of five thousand. Tests read at the level of the business, apply coupon save ten, while the clicks and the HTTP calls hide one level down. That is what makes a framework readable.
interface CouponRule {
boolean applies(long subtotal);
}
record MinSubtotal(long min)
implements CouponRule {
public boolean applies(long s)
{ return s >= min; }
}
new MinSubtotal(5000)
.applies(5000) // true Abstract vs interface
An interview favourite: abstract class or interface? An abstract class can hold fields and real code, and a class can extend only one, which suits base test and base page. An interface is a contract; since Java eight it can have default methods, and a class can implement many, which suits roles like web driver or coupon rule. A short rule: share code with an abstract class, define roles with interfaces.
- abstract class can hold fields and code one parent only (extends) e.g. BaseTest, BasePage
- interface a contract, default methods many per class (implements) e.g. WebDriver, CouponRule
- prefer interfaces for roles
Composition
Inheritance is easy to overuse. A cart test that extends logged in test, which extends API test, which extends base test, becomes hard to change. Composition is usually better: the test has an API client, a users helper and a cart page, each doing one job. You can mix them freely and swap any one. In framework design interviews, saying you prefer composition and keep inheritance shallow is a strong signal.
// deep chains get messy:
// CartTest extends LoggedInTest
// extends ApiTest extends Base
class CartTest {
Api api = new Api(URL);
Users users = new Users(api);
CartPage cart = ...;
}
// each helper does one job static and final
Two keywords appear everywhere. Static means it belongs to the class, not to an object, so it suits constants and small utility methods. Final means it can be set only once, which suits constants and fields like a sku. One warning: a static web driver shared by every test is a common beginner design, and it breaks as soon as tests run in parallel, because all threads fight over one browser. Part five shows thread safe drivers.
static final long FREE_FROM =
7500; // one constant
static long cents(String p)
{...} // utility method
final String sku; // set once
static WebDriver driver;
// shared by every test:
// breaks parallel runs! Common mistakes
OOP mistakes in test frameworks. Public locators that tests use directly, which defeats encapsulation. Deep inheritance chains. A god class, one utils file with two hundred methods. Static drivers or other state shared between tests. Many assertions buried inside page objects, which teams debate, but most keep them in the tests. And mutable test data that one test changes and another depends on.
- public locators used in tests
- deep inheritance chains
- god classes: one Utils with 200 methods
- static driver or state shared between tests
- assertions buried in page objects (debated: keep few)
- mutable test data shared across tests
Say it like this
The classic question is: where do you use OOP in your framework? Map each pillar to something concrete. Encapsulation: page objects hide locators behind methods. Inheritance: a shallow base test handles setup and teardown. Polymorphism: tests use the web driver interface, so the browser is just a setting. Abstraction: tests read as business steps. And add: I prefer composition for helpers, and avoid static shared state so tests can run in parallel.
- "Encapsulation: page objects
- hide locators behind methods.
- Inheritance: a shallow base
- test for setup and teardown.
- Polymorphism: tests use the WebDriver interface, so the browser is a setting.
- Abstraction: tests read as business steps. I prefer
- composition for helpers."