Object Oriented Techniques (OCR A-Level Computer Science): Revision Notes
Object Oriented Techniques
Overview
Object-Oriented Programming (OOP) is a programming paradigm that organises software design around objects, which represent real-world entities. OOP provides a modular approach to software development, making it easier to build, maintain, and scale complex systems. Understanding OOP concepts like classes, objects, methods, encapsulation, inheritance, and polymorphism is crucial for writing efficient and reusable code.
Classes and Objects
Class:
- A blueprint for creating objects.
- Defines the properties (attributes) and behaviours (methods) of an object.
Example in Python:
class Car:
def __init__(self, brand, model):
self.brand = brand # Attribute
self.model = model # Attribute
def display_info(self): # Method
print(f"{self.brand} {self.model}")
Object:
An instance of a class.
Example:
my_car = Car("Toyota", "Corolla")
my_car.display_info() # Outputs: Toyota Corolla
Attributes (Properties)
Attributes store the data or state of an object.
- Public Attributes: Can be accessed directly from outside the class.
- Private Attributes: Prefixed with
_or__(e.g.,_balance). They cannot be accessed directly outside the class.
class Account:
def __init__(self, balance):
self.__balance = balance # Private attribute
Methods
Methods are functions defined within a class that describe the behaviours of an object.
- Instance Methods: Operate on instance attributes.
- Static Methods: Do not depend on instance attributes; defined using
@staticmethod. - Class Methods: Operate on class-level attributes; defined using
@classmethod.
Encapsulation
Definition:
Encapsulation is the principle of restricting direct access to an object's attributes and methods to protect its integrity.
Access Modifiers:
- Public: Accessible from anywhere.
- Private: Accessible only within the class.
Getters and Setters:
Used to access and modify private attributes.
class Account:
def __init__(self, balance):
self.__balance = balance
def get_balance(self): # Getter
return self.__balance
def set_balance(self, amount): # Setter
if amount >= 0:
self.__balance = amount
Inheritance
Definition:
- Inheritance allows a class (subclass) to inherit attributes and methods from another class (superclass or parent class).
- Promotes code reuse and establishes a hierarchical relationship between classes.
Syntax Example:
class Animal:
def speak(self):
print("Animal sound")
class Dog(Animal): # Dog is a subclass of Animal
def speak(self):
print("Bark")
Superclasses and Subclasses:
- Superclass (Parent Class): The class from which other classes inherit.
- Subclass (Child Class): The class that inherits from the superclass.
Using the super() Function:
Allows access to the superclass's methods and attributes.
class Dog(Animal):
def __init__(self, name):
super().__init__()
self.name = name
Polymorphism
Definition:
- Polymorphism allows objects of different classes to be treated as objects of a common superclass.
- Methods can behave differently depending on the object calling them.
Example:
class Animal:
def speak(self):
print("Animal sound")
class Dog(Animal):
def speak(self):
print("Bark")
class Cat(Animal):
def speak(self):
print("Meow")
animals = [Dog(), Cat()]
for animal in animals:
animal.speak()
Outputs:
Bark
Meow
Interpreting Class Diagrams
Class diagrams visually represent the structure of a class, including its attributes, methods, and relationships with other classes.
Example:
+---------------------+
| Car |
+---------------------+
| - brand: String |
| - model: String |
+---------------------+
| + display_info() |
+---------------------+
- +: Public
- -: Private
Real-World Scenario: Library System
Problem:
Design a system for managing books in a library.
Solution Using OOP:
Classes:
Book: Represents individual books.Member: Represents library members.Library: Manages the collection of books and members. Code Implementation:
class Book:
def __init__(self, title, author):
self.title = title
self.author = author
def display(self):
print(f"{self.title} by {self.author}")
class Library:
def __init__(self):
self.books = []
def add_book(self, book):
self.books.append(book)
def show_books(self):
for book in self.books:
book.display()
# Using the system
lib = Library()
book1 = Book("1984", "George Orwell")
book2 = Book("Brave New World", "Aldous Huxley")
lib.add_book(book1)
lib.add_book(book2)
lib.show_books()
Benefits of Object-Oriented Programming
- Modularity: Programmes can be divided into self-contained objects.
- Reusability: Classes can be reused across different programmes.
- Maintainability: Easier to update and debug specific parts of a programme.
- Scalability: OOP makes it easier to manage and expand large projects.
Note Summary
Common Mistakes
- Overusing Inheritance: Using inheritance when composition would be simpler.
- Poor Encapsulation: Allowing unrestricted access to class attributes.
- Ignoring Polymorphism: Not leveraging polymorphism for cleaner and more dynamic code.
Key Takeaways
- Object-Oriented Programming organises code into classes and objects, promoting reusability and scalability.
- Key principles include encapsulation, inheritance, and polymorphism.
- Encapsulation protects object data, while inheritance and polymorphism enable code reuse and flexibility.
- Understanding and implementing these techniques are essential for writing efficient, maintainable, and modular programmes.