You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
This lesson covers the key concepts of object-oriented programming (OOP) and design: classes, objects, inheritance, polymorphism, encapsulation and abstraction. The OCR H446 specification requires you to understand these concepts and apply them to design problems.
| Concept | Definition |
|---|---|
| Class | A blueprint or template that defines the attributes (data) and methods (behaviours) that objects of that type will have |
| Object | A specific instance of a class, with its own values for the class's attributes |
Analogy: A class is like a cookie cutter (the template); objects are the individual cookies (each with its own specific details).
Class: Dog
Attributes: name, breed, age
Methods: bark(), fetch(), sit()
Object 1: Dog("Rex", "Labrador", 5)
Object 2: Dog("Bella", "Poodle", 3)
Each object has its own values for name, breed and age, but they share the same methods defined in the class.
Encapsulation means bundling the data (attributes) and the methods that operate on that data together inside a class, and restricting direct access to the internal state from outside the class.
| Concept | Detail |
|---|---|
| Private attributes | Attributes are declared as private — they cannot be accessed directly from outside the class |
| Public methods (getters/setters) | Controlled access to attributes is provided through public methods |
| Information hiding | The internal implementation is hidden from other parts of the program |
| Benefit | Explanation |
|---|---|
| Data protection | External code cannot set invalid values directly (e.g. a negative age) |
| Controlled access | Setter methods can include validation logic |
| Reduced coupling | Other parts of the program depend on the public interface, not the internal implementation. Changes to internals do not break external code |
| Easier maintenance | Internal changes are contained within the class |
| Modifier | Access |
|---|---|
| Public | Accessible from anywhere |
| Private | Accessible only within the class itself |
| Protected | Accessible within the class and its subclasses |
Inheritance allows a new class (the subclass or child class) to inherit the attributes and methods of an existing class (the superclass or parent class). The subclass can then extend or override the inherited behaviour.
Animal (superclass)
/ \
Dog Cat (subclasses)
/
Labrador (subclass of Dog)
| Term | Definition |
|---|---|
| Superclass (parent) | The class being inherited from |
| Subclass (child) | The class that inherits from the superclass |
| IS-A relationship | A subclass IS-A type of its superclass (e.g. "Dog IS-A Animal") |
| Benefit | Explanation |
|---|---|
| Code reuse | Common attributes and methods are defined once in the superclass and inherited by all subclasses |
| Hierarchical organisation | Classes are organised in a logical hierarchy |
| Extensibility | New subclasses can be added without modifying existing code |
| Reduced redundancy | Avoids duplicating code across similar classes |
A subclass can override a method inherited from its superclass by providing its own implementation with the same name and parameters.
Class Animal:
method speak():
return "..."
Class Dog extends Animal:
method speak(): <-- Overrides Animal.speak()
return "Woof!"
Class Cat extends Animal:
method speak(): <-- Overrides Animal.speak()
return "Meow!"
Polymorphism (meaning "many forms") allows objects of different classes to be treated as objects of a common superclass. The same method call can produce different behaviour depending on the actual type of the object.
| Type | Description |
|---|---|
| Runtime (dynamic) polymorphism | Method overriding — the correct method is determined at runtime based on the actual object type |
| Compile-time (static) polymorphism | Method overloading — multiple methods with the same name but different parameter lists |
animals = [Dog("Rex"), Cat("Whiskers"), Dog("Bella")]
for animal in animals:
print(animal.speak()) <-- Calls Dog.speak() or Cat.speak()
depending on the actual type
Output: "Woof!", "Meow!", "Woof!"
The same method call (animal.speak()) produces different results because each object's class has its own implementation.
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.