Docsity

OOP in 13 Pages – Complete Summary with Java & Python Examples (2026), Summaries of Object Oriented Programming

Partial preview of the text

Download OOP in 13 Pages – Complete Summary with Java & Python Examples (2026) and more Summaries Object Oriented Programming in PDF only on Docsity!

Object-Oriented Programming (OOP) in 10 pages

Complete Summary with Java & Python Examples

Table of Contents

  1. Introduction to OOP
  2. The Four Pillars of OOP ○ 2.1 Encapsulation ○ 2.2 Abstraction ○ 2.3 Inheritance ○ 2.4 Polymorphism
  3. Classes vs. Objects
  4. Code Examples (Java & Python)
  5. Common OOP Design Principles — SOLID
  6. Quick Reference Table

1. Introduction to OOP

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.

2.2 Abstraction

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.

2.3 Inheritance

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!"); }

}

2.4 Polymorphism

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).

3. Classes vs. Objects

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

4. Code Examples — Side-by-Side (Java & Python)

Full Class Example: Car

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

6. Quick Reference Table

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

Key Takeaways

  1. OOP organizes code around objects — bundling data and behavior.
  2. Encapsulation protects state; abstraction simplifies interfaces.
  3. Inheritance enables reuse but should not be overused — prefer composition.
  4. Polymorphism makes code flexible and extensible.
  5. SOLID principles are your guide to clean, maintainable OOP design.
  6. Classes are blueprints; objects are instances — each object has its own state.