Show understanding of the characteristics of a number of programming paradigms: Object Oriented

20.1 Programming Paradigms

Object‑Oriented Programming (OOP) 🚀

Object‑Oriented Programming is a way of organising code around objects – self‑contained units that bundle data (attributes) and behaviour (methods). Think of an object as a mini‑world, like a LEGO set: each piece (object) has its own colour, shape and function, but can also connect to other pieces to build something bigger.

Key Characteristics

Characteristic What It Means Analogy
Encapsulation 🔒 Hides internal state; exposes only a public interface. A sealed toolbox that keeps tools safe.
Abstraction 🧩 Simplifies complex reality by modelling essential features. A video game character that hides the code behind the controller.
Inheritance 🏗️ Creates a new class from an existing one, reusing code. A family tree where children inherit traits from parents.
Polymorphism 🌈 Same method name works differently on different objects. A shape‑shifter that changes form but keeps its name.

Designing a Class: Step‑by‑Step

  1. Identify the real‑world entity you want to model (e.g., Car).
  2. List its attributes (data) – colour, model, speed.
  3. Decide on behaviours (methods) – accelerate(), brake(), honk().
  4. Write the class skeleton in your chosen language.
  5. Implement methods, keeping encapsulation in mind.
  6. Test the object by creating instances and calling methods.

Benefits for Students

  • Modular code that’s easier to read and maintain.
  • Reusable components through inheritance.
  • Clear separation of data and behaviour.
  • Scalable design for large projects.

Quick Example: Car Class (Java‑style)

public class Car {
  private String colour;
  private int speed;

  public Car(String colour) {
    this.colour = colour;
    this.speed = 0;
  }

  public void accelerate() {
    speed += 10;
  }

  public void brake() {
    speed = Math.max(0, speed - 10);
  }

  public void honk() {
    System.out.println("Beep beep!");
  }
}

OOP vs Procedural: Quick Comparison

Aspect Procedural OOP
Focus Functions & data flow Objects & interactions
Reusability Functions can be reused but data is global. Classes can be inherited and extended.
Maintenance Harder to track changes in large code. Encapsulation keeps changes local.

Remember: OOP is like building with LEGO blocks – each block (object) has its own shape and colour, but you can snap them together to create something new and exciting! 🌟

Revision

Log in to practice.

2 views 0 suggestions