OOPs Concepts in Java with Examples

Introduction

Object-Oriented Programming (OOP) in Java is a way of designing software using objects, classes, and real-world modeling.
It makes code modular, reusable, flexible, and easier to maintain.

  • Encapsulation
  • Inheritance
  • Polymorphism
  • Abstraction

1. Class & Object

2. Encapsulation

3. Inheritance

4. Polymorphism

a) Compile-time Polymorphism (Method Overloading)

b) Runtime Polymorphism (Method Overriding)

5. Abstraction

a) Abstract Class Example

b) Interface Example

Summary

  • Encapsulation → Data hiding (getters/setters)
  • Inheritance → Code reusability
  • Polymorphism → Overloading & Overriding
  • Abstraction → Hides complexity

👉 These concepts are heavily used in Selenium:

  • WebDriver is an interface (Abstraction)
  • ChromeDriver, FirefoxDriver implement WebDriver (Polymorphism)
  • Base classes for tests (Inheritance)
  • Utilities & Config classes (Encapsulation)