📚 Table of Contents
🌟 What is Object-Oriented Programming?
Object-Oriented Programming (OOP) is a programming paradigm based on the concept of "objects" that contain both data (fields/attributes) and code (methods/functions). OOP organizes software design around data, or objects, rather than functions and logic.
The Four Pillars of OOP
- Encapsulation - Bundling data and methods together, hiding implementation details
- Inheritance - Creating new classes from existing ones, promoting code reuse
- Polymorphism - Same method name, different behaviors (method overloading/overriding)
- Abstraction - Hiding complex implementation, exposing only essential features
🔒 1. Encapsulation
What is Encapsulation?
Encapsulation is the bundling of data (fields) and methods that operate on that data within a single unit (class), while restricting direct access to some of the object's components. This is achieved using access modifiers.
Access Modifiers in Java
| Modifier | Class | Package | Subclass | World |
|---|---|---|---|---|
| public | ✅ | ✅ | ✅ | ✅ |
| protected | ✅ | ✅ | ✅ | ❌ |
| default (no modifier) | ✅ | ✅ | ❌ | ❌ |
| private | ✅ | ❌ | ❌ | ❌ |
Example from Our Project: BasePage.java
- Data Hiding: Driver and wait objects are protected, preventing external misuse
- Controlled Access: Only child page classes can access these fields
- Maintainability: Changes to internal implementation don't affect external code
Example from LoginPage.java
login() method without knowing about
usernameField or passwordField. The
implementation is hidden (encapsulated).
🧬 2. Inheritance
What is Inheritance?
Inheritance is a mechanism where a new class (child/subclass) derives properties and behaviors from an existing class (parent/superclass). It promotes code reusability and establishes an "is-a" relationship.
Inheritance Hierarchy in Our Project
(Parent Class)
(Child Class)
(Child Class)
Example: BasePage (Parent Class)
Example: LoginPage (Child Class)
-
Code Reusability:
clickElement()andenterText()written once, used everywhere - Single Point of Change: Update wait logic in BasePage, all pages benefit
- Consistency: All pages use same interaction pattern
- Reduced Duplication: No need to rewrite common methods in each page class
Example: BaseTest Inheritance
super(driver) calls BasePage
constructor before LoginPage constructor executes.
🎭 3. Polymorphism
What is Polymorphism?
Polymorphism (Greek: "many forms") allows objects to take on multiple forms. The same method name can behave differently based on the object calling it or the parameters passed. There are two types:
- Method Overloading (Compile-time polymorphism) - Same method name, different parameters
- Method Overriding (Runtime polymorphism) - Child class redefines parent method
Method Overloading in Our Project
Our BasePage demonstrates method overloading for both Selenium and Playwright:
- Java determines which method to call based on the parameter type
-
If you pass a
WebElement, Selenium version is called -
If you pass a
Locator, Playwright version is called - Same method name = Cleaner, more intuitive API
Method Overloading in LoginPage
Usage in Test Class
- Overloading (Compile-time): Compiler decides which method to call based on parameters
- Overriding (Runtime): JVM decides which method to call based on object type
Constructor Overloading
| Aspect | Method Overloading | Method Overriding |
|---|---|---|
| Definition | Same method name, different parameters | Same signature in parent and child class |
| When | Compile-time (Static) | Runtime (Dynamic) |
| Where | Same class | Parent and child class |
| Return Type | Can be different | Must be same or covariant |
🎨 4. Abstraction
What is Abstraction?
Abstraction is the concept of hiding complex implementation details and showing only essential features. It focuses on what an object does rather than how it does it.
Abstraction in Our Framework
Our Page Object Model demonstrates abstraction at multiple levels:
Level 1: BasePage Abstraction
Level 2: LoginPage Abstraction
Level 3: Test Class - Clean and Simple
- Simplicity: Tests read like plain English, easy to understand
- Maintainability: UI changes only affect Page classes, not tests
- Reduced Complexity: Each layer handles its own concerns
- Reusability: Same methods used across multiple tests
- Focus: Tests focus on "what" not "how"
Abstraction Layers Diagram
(What to test)
(Business actions)
(Common operations)
(Browser automation)
🌍 Real-World Benefits in Our Framework
Before OOP (Procedural Approach)
After OOP (With Page Object Model)
Maintainability Example
Scenario: The login button ID changes from
submit to login-btn
| Without OOP | With OOP |
|---|---|
|
❌ Update 15 test files ❌ Find all instances ❌ Risk missing some ❌ Time: 30-60 minutes |
✅ Update LoginPage only ✅ One line change ✅ All tests work ✅ Time: 1 minute |
How OOP Principles Work Together
The Perfect Combination
- Encapsulation hides WebDriver complexity in BasePage
- Inheritance allows LoginPage and SecurePage to reuse BasePage methods
- Polymorphism enables same methods for both Selenium and Playwright
- Abstraction lets tests focus on business logic, not technical details
Result: Clean, maintainable, scalable test automation framework!
Key Takeaways
- DRY Principle: Don't Repeat Yourself - write once, use everywhere
- Single Responsibility: Each class has one clear purpose
- Open/Closed Principle: Open for extension, closed for modification
- Maintainability: Changes are localized and easy to implement
- Scalability: Easy to add new pages and tests
- Readability: Code is self-documenting and easy to understand
- Team Collaboration: Clear structure helps teams work together
📋 Summary
| OOP Principle | Definition | Example in Our Project | Benefit |
|---|---|---|---|
| Encapsulation | Bundling data and methods, hiding internals | Private fields in LoginPage, protected methods in BasePage | Data protection, controlled access |
| Inheritance | Child class derives from parent class | LoginPage extends BasePage, LoginTest extends BaseTest | Code reuse, reduced duplication |
| Polymorphism | Same name, multiple forms | Overloaded methods for Selenium and Playwright | Flexibility, unified interface |
| Abstraction | Hide complexity, show essentials | Page Object Model hides WebDriver details | Simplicity, focus on business logic |
- Study the code in each class to see these principles in action
- Try modifying a Page class and see how tests remain unaffected
- Add a new page object using the same OOP patterns
- Practice explaining each principle to reinforce your understanding