







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!








Object-Oriented Programming (OOP) is a programming paradigm that organizes software design around objects — self-contained units that bundle data (attributes) and behavior (methods) together.
OOP vs. Procedural Programming:
Aspect Procedural OOP
Organization Around functions Around objects
Data access Often global/shared Encapsulated within objects
Reusability Limited High (via
inheritance/composition)
Scalability Harder for large systems Naturally modular
Key Benefits of OOP:
● Modularity — Code is organized into discrete, independent units ● Reusability — Classes can be reused across projects and extended ● Maintainability — Changes in one class rarely ripple across the system ● Scalability — Large systems are easier to design and grow
One-sentence definition: OOP is a paradigm where programs are built from objects that encapsulate state and behavior, interacting through well-defined interfaces.
Common Mistake: Making all fields public. This breaks encapsulation and allows uncontrolled state changes.
Definition: Hiding complex implementation details and exposing only the essential features to the user.
Purpose:
● Simplify usage — the user doesn't need to understand internals ● Allow implementation changes without affecting the interface ● Reduce cognitive load
Example (Java):
java
// User only sees the interface, not the implementation List names = new ArrayList<>(); names.add("Alice"); names.add("Bob");
// User doesn't know (or care) that ArrayList uses a dynamic array
internally
Example (Python):
python
from abc import ABC, abstractmethod
class Shape(ABC): @abstractmethod def area(self): pass
class Circle(Shape): def init(self, r): self.r = r
def area(self): return 3.14159 * self.r ** 2
Abstraction vs. Encapsulation: Abstraction is about what is exposed (the interface). Encapsulation is about how access is controlled (the mechanism). They work together but are not the same.
Common Mistake: Confusing abstraction with encapsulation. Remember: abstraction = design level; encapsulation = implementation level.
Definition: A mechanism where a new class (subclass/child) acquires the properties and behaviors of an existing class (superclass/parent).
Purpose:
● Code reuse — avoid duplicating common logic ● Hierarchical classification — model "is-a" relationships ● Extensibility — add or override behavior in subclasses
Example (Java):
java
class Animal { void speak() { System.out.println("..."); } }
class Dog extends Animal { @Override void speak() { System.out.println("Woof!"); } }
class Cat extends Animal { @Override void speak() { System.out.println("Meow!"); }
}
Definition: The ability of different objects to respond to the same method call in different ways.
Purpose:
● Flexibility — write code that works with multiple types ● Extensibility — add new types without changing existing code ● Decoupling — client code depends on interfaces, not concrete classes
Types:
Type Also Called When
Resolved
Example
Compile-time Method
overloading
At compile time
add(int, int) vs add(double, double)
Runtime Method overriding At runtime animal.speak() →
depends on actual object type
Example (Java) — Runtime Polymorphism:
java
Animal a = new Dog(); a.speak(); // Output: "Woof!" — decided at runtime
a = new Cat();
a.speak(); // Output: "Meow!"
Example (Python) — Duck Typing:
python
def make_it_speak(animal): print(animal.speak())
make_it_speak(Dog()) # Woof! make_it_speak(Cat()) # Meow!
# Python doesn't require a common parent — if it has .speak(), it works
Common Mistake: Confusing overloading (same name, different parameters, compile-time) with overriding (same signature, subclass replaces parent behavior, runtime).
Concept Class Object
Definition A blueprint or template An instance of a class
Existence Defined at design time Created at runtime
State No concrete values Has specific attribute values
Analogy Cookie cutter The cookie itself
Java:
java
public class Car { private String brand; private int speed;
public Car(String brand) { this.brand = brand; this.speed = 0; }
public void accelerate() { speed += 10; } public void brake() { speed = Math.max(0, speed - 10); } public int getSpeed() { return speed; } public String getBrand() { return brand; }
public static void main(String[] args) { Car c = new Car("Toyota"); c.accelerate(); c.accelerate(); System.out.println(c.getBrand() + " is going " + c.getSpeed() + " km/h"); // Output: Toyota is going 20 km/h }
}
Python:
python
class Car: def init(self, brand): self.__brand = brand self.__speed = 0
def accelerate(self): self.__speed += 10
def brake(self): self.__speed = max(0, self.__speed - 10)
@property def speed(self): return self.__speed
@property def brand(self): return self.__brand
c = Car("Toyota") c.accelerate() c.accelerate() print(f"{c.brand} is going {c.speed} km/h")
# Output: Toyota is going 20 km/h
What to notice:
● private in Java ↔ __ prefix in Python (name mangling) ● getSpeed() in Java ↔ @property in Python ● Both enforce encapsulation and provide controlled access
Pillar Definition (Short) Real-World Analogy Common Mistake
Encapsulat
ion
Hide internal state, expose controlled access
Capsule pill — inside is protected
Public fields everywhere
Abstractio
n
Expose only essential features
Car pedals — you don't see the engine
Confusing with encapsulation
Inheritance Reuse via
parent-child hierarchy
Parent → child traits Overusing "is-a"; prefer composition
Polymorphi
sm
Same call, different behavior
Universal remote works on many TVs
Mixing overloading with overriding