π Object-Oriented Programming (OOP) Study Guide
1οΈβ£ What is OOP?
Object-Oriented Programming (OOP) is a way of designing and structuring code by grouping related data (attributes) and functions (methods) inside objects. It makes code easier to manage, reuse, and scale.
β **Real-life Example:** Think of a **Car** as an object. It has:
- Attributes (Data): Color, Model, Speed.
- Methods (Functions): Start(), Stop(), Accelerate().
In OOP, we create objects based on **blueprints (Classes)**.
2οΈβ£ Core Principles of OOP
OOP is based on 4 main principles:
πΉ 1. Encapsulation (Data Protection)
Encapsulation means **hiding internal details** and allowing controlled access to data using methods.
β **Example:** A **Bank Account** hides the balance, but you can access it using a method.
class BankAccount {
private double balance; // Hidden from direct access
public void deposit(double amount) {
balance += amount;
}
public double getBalance() {
return balance; // Controlled access
}
}
πΉ 2. Abstraction (Hiding Complexity)
Abstraction means **hiding unnecessary details** and showing only essential features.
β **Example:** When you drive a **car**, you just press the acceleratorβ you don't need to know how the engine works internally.
abstract class Animal {
abstract void makeSound(); // Hides internal details
}
class Dog extends Animal {
void makeSound() {
System.out.println("Bark!"); // Implementation shown to the user
}
}
πΉ 3. Inheritance (Code Reusability)
Inheritance allows a new class (**child class**) to use properties and methods of an existing class (**parent class**).
β **Example:** A **Car** inherits features from **Vehicle**.
class Vehicle {
String brand = "Toyota";
}
class Car extends Vehicle {
String model = "Corolla";
}
πΉ 4. Polymorphism (One Name, Many Forms)
Polymorphism allows the same function to perform different tasks.
β **Example:** A **remote control** has one button, but it can operate a **TV, AC, or Fan**.
// Method Overriding (Different behavior for same method)
class Animal {
void makeSound() {
System.out.println("Some sound...");
}
}
class Cat extends Animal {
void makeSound() {
System.out.println("Meow!");
}
}
3οΈβ£ More OOP Concepts
πΉ Method Overloading (Multiple Functions with the Same Name)
Allows a method to have different versions.
class MathOperations {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
}
πΉ Method Overriding (Changing Behavior of a Parent Class Method)
The child class provides a new version of the parent class method.
class Parent {
void show() {
System.out.println("Parent class");
}
}
class Child extends Parent {
void show() {
System.out.println("Child class");
}
}
πΉ Interfaces and Abstract Classes
Interfaces define a contract for classes, and abstract classes provide partial implementations.
interface Vehicle {
void start();
}
class Bike implements Vehicle {
public void start() {
System.out.println("Bike is starting...");
}
}
4οΈβ£ Common OOP Design Patterns
πΉ Singleton Pattern (Only One Instance of a Class)
Ensures a class has **only one object** throughout the program.
class Singleton {
private static Singleton instance;
private Singleton() {}
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
πΉ Factory Pattern (Creating Objects without Exposing the Logic)
Creates objects based on input without revealing the actual class used.
class ShapeFactory {
static Shape getShape(String type) {
if (type.equals("Circle")) return new Circle();
if (type.equals("Square")) return new Square();
return null;
}
}
5οΈβ£ SOLID Principles (Good OOP Practices)
πΉ Single Responsibility Principle (SRP)
A class should have **only one reason to change**.
class Invoice {
void calculateTotal() {}
}
class InvoicePrinter {
void printInvoice(Invoice invoice) {}
}
πΉ Open-Closed Principle (OCP)
A class should be **open for extension but closed for modification**.
interface Payment {
void pay();
}
class CreditCard implements Payment {
public void pay() {
System.out.println("Paid with Credit Card");
}
}
6οΈβ£ Conclusion
OOP makes programming **easier to understand, maintain, and reuse**. Mastering OOP principles helps build scalable and efficient applications.