QA AND TEST AUTOMATION · PART 2 · PROGRAMMING FOR TESTERS · JAVA · 22 OF 112

OOP Concepts for Test Automation, in Java

Classes and objects, constructors, and the four pillars (encapsulation, inheritance, polymorphism, abstraction) explained through the test code you will actually write: a ShopRung page object, a base test class, the WebDriver interface, and records for test data. Plus composition over inheritance, and the OOP questions every SDET interview asks.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 67
  63. 68
  64. 69
  65. 70
  66. 71
  67. 72
  68. 73
  69. 74
  70. 75
  71. 76
  72. 77
  73. 78
  74. 79
  75. 80
  76. 81
  77. 82
  78. 83
  79. 84
  80. 85
  81. 86
  82. 87
  83. 88
  84. 89
  85. 90
  86. 91
  87. 92
  88. 93
  89. 94
  90. 95
  91. 96
  92. 97
  93. 98
  94. 99
  95. 100
  96. 101
  97. 102
  98. 103
  99. 104
  100. 105
  101. 106
  102. 107
  103. 108
  104. 109
  105. 110
  106. 111
  107. 112
1 / 13

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • Classes, objects, records
  • encapsulation in page objects
  • base tests
  • WebDriver polymorphism
  • composition

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.

A class and objects
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.

Java records (Java 16+)
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.

A page object
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.

A base test class
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 is an interface
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.

Interfaces and abstract classes
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.

Which one?
  • 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.

Composition over inheritance
// 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 and final
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.

Avoid these
  • 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.

In the interview
  • "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."

Interview questions

Q1What are the four pillars of OOP?

Encapsulation (hide data behind methods), inheritance (reuse a parent class), polymorphism (one interface, many implementations, plus overriding) and abstraction (expose what, hide how).

Q2Where is encapsulation used in test automation?

In page objects: locators and page details are private, and tests call methods like applyCoupon() or total(), so UI changes are fixed in one place.

Q3What is the difference between an abstract class and an interface?

An abstract class can have fields, constructors and implemented methods, and a class can extend only one. An interface defines a contract (with optional default methods) and a class can implement many.

Q4How is polymorphism used with Selenium?

WebDriver is an interface implemented by ChromeDriver, FirefoxDriver and others; tests code against WebDriver, so the browser is chosen at runtime.

Q5What is the difference between overloading and overriding?

Overloading is several methods with the same name but different parameters in one class (compile time). Overriding is a subclass replacing a parent method with the same signature (runtime).

Q6Why prefer composition over inheritance?

Composition combines small single-purpose helpers and avoids rigid, deep hierarchies; it is easier to change, test and reuse.

Q7Why is a static WebDriver a problem?

It is shared by all tests, so parallel tests interfere with each other. Use one driver per test or per thread (for example ThreadLocal).

Q8What is a Java record?

A compact, immutable data class (Java 16+) whose constructor, accessors, equals, hashCode and toString are generated; ideal for test data and expected values.