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
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
class Car { String color; int speed; void drive() { System.out.println(color + " car is driving at " + speed + " km/h"); } } public class Main { public static void main(String[] args) { Car car1 = new Car(); car1.color = "Red"; car1.speed = 80; car1.drive(); } } |
2. Encapsulation
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
class BankAccount { private double balance; public void deposit(double amount) { balance += amount; } public double getBalance() { return balance; } } public class Main { public static void main(String[] args) { BankAccount acc = new BankAccount(); acc.deposit(1000); System.out.println("Balance: " + acc.getBalance()); } } |
3. Inheritance
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
class Animal { void sound() { System.out.println("Animal makes a sound"); } } class Dog extends Animal { void sound() { System.out.println("Dog barks"); } } public class Main { public static void main(String[] args) { Dog d = new Dog(); d.sound(); // Dog barks } } |
4. Polymorphism
a) Compile-time Polymorphism (Method Overloading)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
class Calculator { int add(int a, int b) { return a + b; } double add(double a, double b) { return a + b; } } public class Main { public static void main(String[] args) { Calculator c = new Calculator(); System.out.println(c.add(2, 3)); System.out.println(c.add(2.5, 3.5)); } } |
b) Runtime Polymorphism (Method Overriding)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
class Shape { void draw() { System.out.println("Drawing shape"); } } class Circle extends Shape { void draw() { System.out.println("Drawing circle"); } } public class Main { public static void main(String[] args) { Shape s = new Circle(); s.draw(); // Drawing circle } } |
5. Abstraction
a) Abstract Class Example
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
abstract class Vehicle { abstract void start(); void fuelType() { System.out.println("Petrol or Diesel"); } } class Bike extends Vehicle { void start() { System.out.println("Bike starts with Kick"); } } public class Main { public static void main(String[] args) { Vehicle v = new Bike(); v.start(); v.fuelType(); } } |
b) Interface Example
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
interface WebDriver { void openBrowser(); void closeBrowser(); } class ChromeDriver implements WebDriver { public void openBrowser() { System.out.println("Chrome opened"); } public void closeBrowser() { System.out.println("Chrome closed"); } } public class Main { public static void main(String[] args) { WebDriver driver = new ChromeDriver(); driver.openBrowser(); driver.closeBrowser(); } } |
Summary
- Encapsulation → Data hiding (getters/setters)
- Inheritance → Code reusability
- Polymorphism → Overloading & Overriding
- Abstraction → Hides complexity
👉 These concepts are heavily used in Selenium:
WebDriveris an interface (Abstraction)ChromeDriver,FirefoxDriverimplement WebDriver (Polymorphism)- Base classes for tests (Inheritance)
- Utilities & Config classes (Encapsulation)