Procedural vs Object-Oriented Programming — Key Differences
Before OOP, most programs were written in a procedural style — a sequence of instructions organized into functions. Let’s compare both approaches with a concrete example.
The Same Problem, Two Approaches
Task: Manage employee records — store name, salary, and calculate bonuses.
Procedural Approach
// Data is separate from functions
String[] names = {"Alice", "Bob"};
double[] salaries = {50000, 60000};
static double calculateBonus(double salary) {
return salary * 0.10;
}
static void printEmployee(String name, double salary) {
System.out.println(name + " - $" + salary +
" (Bonus: $" + calculateBonus(salary) + ")");
}
Problems:
- Data (
names[],salaries[]) and functions (calculateBonus) are separate - Arrays must be kept in sync manually —
names[0]must correspond tosalaries[0] - Adding a field (e.g., department) means modifying every function
OOP Approach
class Employee {
String name;
double salary;
Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
double calculateBonus() {
return salary * 0.10;
}
void print() {
System.out.printf("%s - $%.0f (Bonus: $%.0f)%n",
name, salary, calculateBonus());
}
}
// Usage
Employee alice = new Employee("Alice", 50000);
alice.print();
Benefits:
- Data and behavior are together
- Each employee is a self-contained object
- Adding a field is easy — change the class, nothing else breaks
Side-by-Side Comparison
| Aspect | Procedural | OOP |
|---|---|---|
| Organization | Functions + data structures | Classes + objects |
| Data access | Global or passed to functions | Encapsulated in objects |
| Code reuse | Copy-paste or function libraries | Inheritance + composition |
| Adding features | Modify many functions | Extend with new classes |
| Scalability | Difficult past ~1000 lines | Scales to millions of lines |
| Real-world modeling | Weak | Natural |
When Procedural Is Fine
Procedural programming isn’t always bad:
- Small scripts — a 50-line utility doesn’t need classes
- Simple data processing — read file, transform, write file
- Performance-critical code — no object overhead
- Functional tasks — pure data transformations
When OOP Shines
- Large applications — enterprise software, web apps
- Team projects — multiple developers, clear module boundaries
- Long-lived codebases — maintained for years
- Complex domains — banking, healthcare, e-commerce
Quick Exercise
Take this procedural code and refactor it into OOP:
// Procedural
String[] bookTitles = {"Java Basics", "OOP Guide"};
String[] bookAuthors = {"Alice", "Bob"};
double[] bookPrices = {29.99, 39.99};
static void printBook(int index) {
System.out.println(bookTitles[index] + " by " +
bookAuthors[index] + " - $" + bookPrices[index]);
}
Summary
- Procedural = functions + separate data
- OOP = objects that combine data + behavior
- OOP is better for large, complex, team-based projects
- Procedural is fine for small scripts and simple tasks
What’s Next?
Now let’s get an overview of the Four OOP Principles before diving deep into each one.