Welcome to the World of Programming Paradigms!
Hello there! Today, we are diving into Programming Paradigms. Don't let the fancy name scare you—a paradigm is simply a "style" or "way of thinking" about how to write computer programs.
Imagine you are building a house. You could build it brick-by-brick (Procedural), or you could buy pre-made rooms and snap them together (Object-Oriented). Both ways build a house, but the way you think about the project is different. In this chapter, we’ll explore the two main styles you need for your AQA A Level: Procedural and Object-Oriented.
1. What is a Programming Paradigm?
At its simplest, a programming paradigm is a classification of programming languages based on their features and the way they structure code.
Quick Review:
- Procedural: Focuses on the logic and actions (verbs).
- Object-Oriented (OOP): Focuses on the data and objects (nouns).
2. Procedural-Oriented Programming
Procedural programming is like a recipe. You start at the top and follow a sequence of instructions (procedures or subroutines) until you reach the end. It uses a structured approach to program design.
The Structured Approach
This means breaking a large, complex problem down into smaller, manageable chunks. This is often called top-down design.
Key Features:
- Sequence: Running instructions one after another.
- Selection: Using IF statements to make decisions.
- Iteration: Using loops (FOR, WHILE) to repeat tasks.
- Subroutines: Giving a name to a block of code so you can use it again and again.
Hierarchy Charts
When we design procedural programs, we use hierarchy charts. These look like a family tree. The main program is at the top, and it branches down into smaller sub-tasks.
Example: A "Game" top-level block might branch down into "Initialize Graphics," "Player Input," and "Update Score."
Advantages of the Structured Approach
1. Easier to Debug: Since the code is in small blocks, it's easier to find exactly where an error is hiding. 2. Code Reuse: You can write a procedure once and use it many times. 3. Teamwork: Different programmers can work on different sub-tasks at the same time.
Key Takeaway: Procedural programming is about doing. It uses a top-down, structured approach to break big problems into smaller procedures.
3. Object-Oriented Programming (OOP)
OOP is a bit different. Instead of focusing on the steps to solve a problem, we focus on the "things" (objects) involved.
Don't worry if this seems tricky at first! Most students find OOP a bit "upside-down" until they see a few examples.
The Big Three: Classes, Objects, and Instantiation
- Class: A blueprint or template. It defines what data an object will have and what it can do.
- Object: A specific "thing" built from the blueprint.
- Instantiation: The process of actually creating an object from a class.
Analogy: A Class is like a cookie cutter. An Object is the actual cookie. Instantiation is the act of pressing the cutter into the dough to make a cookie!
Core Concepts of OOP
- Encapsulation: Hiding the internal data of an object and only allowing it to be accessed through specific methods. This keeps the data safe from accidental changes. Think of it like a capsule protecting the medicine inside.
- Inheritance: When a new class (subclass) takes on the properties and methods of an existing class (superclass).
Example: A Dog class can inherit from an Animal class. A Dog "is-a" Animal. - Polymorphism: This means "many forms." It allows different classes to have a method with the same name but different behaviors.
- Overriding: When a subclass replaces a method it inherited from its parent with its own version.
Example: Both a Cat and a Dog might inherit a makeNoise() method, but the Dog overrides it to "Bark" while the Cat overrides it to "Meow."
Relationships: Aggregation vs. Composition
Objects often interact with each other. We use two main types of "has-a" relationships:
1. Aggregation: A "weak" relationship. If the parent object is destroyed, the child object can still exist.
Example: A Library and Books. If the library closes, the books still exist.
UML Symbol: Shown with a white (hollow) diamond line.
2. Composition: A "strong" relationship. If the parent is destroyed, the child is destroyed too.
Example: A Building and its Rooms. If the building is demolished, the rooms are gone too!
UML Symbol: Shown with a black (filled) diamond line.
Did you know? Using composition instead of inheritance is often better because it makes your code more flexible. This is a common exam tip!
4. OOP Design Principles
To write "good" OOP code, you should follow these three principles mentioned in your syllabus:
- Encapsulate what varies: If a part of your program changes often, put it in its own class so it doesn't break everything else.
- Favor composition over inheritance: It's often easier to build objects out of other objects (Composition) than to create long, complex family trees (Inheritance).
- Program to interfaces, not implementation: Focus on what an object should do, rather than exactly how it does it.
5. Writing and Representing OOP
In your exams, you might need to interpret or draw Class Diagrams.
Access Specifiers
These tell us who can see the data or methods inside a class: - Public (+): Anyone can access it. - Private (-): Only the class itself can see it. (Best for Encapsulation!) - Protected (#): Only the class and its "children" (subclasses) can see it.
Types of Methods
- Static: A method that belongs to the Class itself, not any specific object. You can use it without creating an object first.
- Virtual: A method that can be overridden by a subclass.
- Abstract: A method that must be overridden because it has no code of its own—it's just a placeholder.
Key Takeaway: OOP is about things. It uses classes (blueprints) to create objects that interact through inheritance, polymorphism, and encapsulation.
Summary Checklist
Quick Review:
- Can you explain the difference between a class and an object?
- Do you know the difference between the white diamond (aggregation) and black diamond (composition)?
- Can you list the benefits of the structured approach in procedural programming?
- Do you remember that + means public and - means private?
Don't panic! These terms take practice. Try drawing a class diagram for a simple system like a "School" (with Student and Teacher classes) to help these concepts stick!