OOP Notes
A detailed comparison between procedural and object-oriented programming paradigms, with examples showing when to use each approach.
// Functions operate on data passed to them void addGrade(struct Student *s, int grade) { if (s->gradeCount < 10) { s->grades[s->gradeCount] = grade; s->gradeCount++; } }
float calculateAverage(struct Student *s) { if (s->gradeCount == 0) return 0.0; int sum = 0; for (int i = 0; i < s->gradeCount; i++) { sum += s->grades[i]; } return (float)sum / s->gradeCount; }
char getLetterGrade(float average) { if (average >= 90) return 'A'; if (average >= 80) return 'B'; if (average >= 70) return 'C'; if (average >= 60) return 'D'; return 'F'; }
void printReport(struct Student *s) { float avg = calculateAverage(s); printf("Student: %s\n", s->name); printf("Average: %.1f\n", avg); printf("Grade: %c\n", getLetterGrade(avg)); }
int main() { struct Student alice; strcpy(alice.name, "Alice"); alice.gradeCount = 0;
addGrade(&alice, 85); addGrade(&alice, 92); addGrade(&alice, 78);
printReport(&alice); return 0; }
public class Student { private String name; private List<Integer> grades;
public Student(String name) { this.name = name; this.grades = new ArrayList<>(); }
public void addGrade(int grade) { if (grade < 0 || grade > 100) { throw new IllegalArgumentException("Grade must be 0-100"); } grades.add(grade); }
public double getAverage() { if (grades.isEmpty()) return 0.0; int sum = 0; for (int grade : grades) { sum += grade; } return (double) sum / grades.size(); }
public char getLetterGrade() { double avg = getAverage(); if (avg >= 90) return 'A'; if (avg >= 80) return 'B'; if (avg >= 70) return 'C'; if (avg >= 60) return 'D'; return 'F'; }
public void printReport() { System.out.println("Student: " + name); System.out.println("Average: " + String.format("%.1f", getAverage())); System.out.println("Grade: " + getLetterGrade()); }
public static void main(String[] args) { Student alice = new Student("Alice"); alice.addGrade(85); alice.addGrade(92); alice.addGrade(78); alice.printReport(); } }
## Key Differences Explained
### 1. Data Protection
In procedural code, anyone can modify `alice.gradeCount` directly — even setting it to -5. In OOP, `grades` is private and can only be modified through `addGrade()`, which validates input.
### 2. Code Organization
Procedural code scatters related functionality across many functions. OOP keeps everything about a Student inside the Student class. When your project grows to 50 entity types, this difference becomes enormous.
### 3. Extensibility// OOP: Easy to extend without breaking existing code class GraduateStudent extends Student { private String thesisTitle; private String advisor;
public GraduateStudent(String name, String advisor) { super(name); this.advisor = advisor; }
@Override public char getLetterGrade() { // Graduate students need higher scores double avg = getAverage(); if (avg >= 93) return 'A'; if (avg >= 85) return 'B'; if (avg >= 77) return 'C'; return 'F'; // No D in graduate school } }
In procedural code, you'd need to add `studentType` parameters to every function or create separate functions for each type.
### 4. State Managementtotal_students = 0 all_grades = {} student_names = []
def add_student(name): global total_students student_names.append(name) all_grades[name] = [] total_students += 1
OOP: State is encapsulated within objects
class Classroom: def __init__(self): self.students = []
def add_student(self, student): self.students.append(student)
def get_class_average(self): if not self.students: return 0 total = sum(s.get_average() for s in self.students) return total / len(self.students)
class DataProcessor: """OOP for organization and state""" def __init__(self, filename): self.filename = filename self.data = []
def load(self): with open(self.filename) as f: self.data = f.readlines()
def process(self): # Procedural-style helper function for a simple transformation self.data = [clean_line(line) for line in self.data]
Simple procedural helper — doesn't need to be in a class
def clean_line(line): return line.strip().lower().replace('\t', ' ')
| Aspect | Procedural | OOP |
|---|---|---|
| Focus | Functions/procedures | Objects/classes |
| Data | Separate from functions | Bundled with methods |
| Security | Data exposed globally | Data hidden (encapsulation) |
| Reuse | Function libraries | Inheritance, composition |
| Best for | Small, sequential tasks | Large, complex systems |
| Learning curve | Lower | Higher |
| Maintenance | Harder at scale | Easier at scale |
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Procedural vs Object-Oriented Programming.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Object Oriented Programming (OOP) topic.
Search Terms
object-oriented-programming, object oriented programming (oop), object, oriented, programming, introduction, procedural, oop
Related Object Oriented Programming (OOP) Topics