Fatskills
Practice. Master. Repeat.
Study Guide: UK K12 GCSE/A-Level: Year 12 A-Level Lower Sixth Computer Science - Object-Oriented Programming, Python Classes
Source: https://www.fatskills.com/as-and-a2-levels/chapter/uk-k12-gcse-a-level-year-12-a-level-lower-sixth-computer-science-object-oriented-programming-python-classes

UK K12 GCSE/A-Level: Year 12 A-Level Lower Sixth Computer Science - Object-Oriented Programming, Python Classes

By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.

⏱️ ~10 min read

Learning objectives

Upon completing this topic, students will be able to:

  • Define and explain the concept of object-oriented programming (OOP) in Python.
  • Identify and create Python classes, including attributes and methods.
  • Understand and apply inheritance, polymorphism, and encapsulation in Python classes.
  • Use Python classes to model real-world objects and systems.
  • Analyze and evaluate the benefits and limitations of using OOP in software development.

Core concepts

Object-oriented programming (OOP) is a paradigm that organizes software design around objects and their interactions. In Python, OOP is implemented using classes and objects. A class is a template for creating objects, while an object is an instance of a class.

A Python class is defined using the class keyword followed by the class name and a colon. For example:

class Car:
    pass

The pass statement is a placeholder for code that will be added later.

A class can have attributes, which are data members that describe the object, and methods, which are functions that operate on the object. Attributes are defined using the self parameter, which refers to the current instance of the class. Methods are defined using the def keyword followed by the method name and a colon.

For example:

class Car:
    def __init__(self, color, speed):
        self.color = color
        self.speed = speed

    def accelerate(self):
        self.speed += 10

In this example, the Car class has two attributes, color and speed, and one method, accelerate.

Inheritance is a mechanism that allows one class to inherit the attributes and methods of another class. The inheriting class is called the subclass, while the class being inherited from is called the superclass. In Python, inheritance is implemented using the class keyword followed by the subclass name and a colon, and the ( and ) parentheses containing the superclass name.

For example:

class ElectricCar(Car):
    def __init__(self, color, speed, battery_capacity):
        super().__init__(color, speed)
        self.battery_capacity = battery_capacity

In this example, the ElectricCar class inherits the attributes and methods of the Car class and adds a new attribute, battery_capacity.

Polymorphism is the ability of an object to take on multiple forms. In Python, polymorphism is implemented using method overriding or method overloading. Method overriding occurs when a subclass provides a different implementation of a method that is already defined in its superclass. Method overloading occurs when multiple methods with the same name but different parameters are defined.

For example:

class Shape:
    def area(self):
        pass

class Circle(Shape):
    def __init__(self, radius):
        self.radius = radius

    def area(self):
        return 3.14 * self.radius  2

class Rectangle(Shape):
    def __init__(self, width, height):
        self.width = width
        self.height = height

    def area(self):
        return self.width * self.height

In this example, the Circle and Rectangle classes override the area method of the Shape class to provide their own implementations.

Encapsulation is the idea of bundling data and methods that operate on that data within a single unit, such as a class. In Python, encapsulation is implemented using private attributes and methods, which are denoted by a single underscore prefix.

For example:

class BankAccount:
    def __init__(self, balance):
        self.__balance = balance

    def deposit(self, amount):
        self.__balance += amount

    def get_balance(self):
        return self.__balance

In this example, the BankAccount class encapsulates the balance attribute and provides methods to deposit and retrieve the balance.

Worked examples

Example 1: Creating a Python class

Suppose we want to create a class to represent a bank account. We can define the class as follows:

class BankAccount:
    def __init__(self, balance):
        self.balance = balance

    def deposit(self, amount):
        self.balance += amount

    def withdraw(self, amount):
        if amount > self.balance:
            print("Insufficient funds")
        else:
            self.balance -= amount

    def get_balance(self):
        return self.balance

We can create an instance of the class as follows:

account = BankAccount(1000)
print(account.get_balance())  # Output: 1000
account.deposit(500)
print(account.get_balance())  # Output: 1500
account.withdraw(200)
print(account.get_balance())  # Output: 1300

Example 2: Inheritance

Suppose we want to create a subclass of the BankAccount class to represent a savings account. We can define the subclass as follows:

class SavingsAccount(BankAccount):
    def __init__(self, balance, interest_rate):
        super().__init__(balance)
        self.interest_rate = interest_rate

    def add_interest(self):
        self.balance += self.balance * self.interest_rate / 100

We can create an instance of the subclass as follows:

savings_account = SavingsAccount(1000, 5)
print(savings_account.get_balance())  # Output: 1000
savings_account.add_interest()
print(savings_account.get_balance())  # Output: 1050

Example 3: Polymorphism

Suppose we want to create a class to represent a shape, and we want to override the area method to provide different implementations for different shapes. We can define the class as follows:

class Shape:
    def area(self):
        pass

class Circle(Shape):
    def __init__(self, radius):
        self.radius = radius

    def area(self):
        return 3.14 * self.radius  2

class Rectangle(Shape):
    def __init__(self, width, height):
        self.width = width
        self.height = height

    def area(self):
        return self.width * self.height

We can create instances of the classes and call the area method as follows:

circle = Circle(5)
print(circle.area())  # Output: 78.5
rectangle = Rectangle(4, 6)
print(rectangle.area())  # Output: 24

Common misconceptions

  • Misconception 1: A class is a type of object.
  • Why the distractor fails: A class is a template for creating objects, not an object itself.
  • Misconception 2: Inheritance is a form of polymorphism.
  • Why the distractor fails: Inheritance is a mechanism that allows one class to inherit the attributes and methods of another class, while polymorphism is the ability of an object to take on multiple forms.
  • Misconception 3: Encapsulation is the same as abstraction.
  • Why the distractor fails: Encapsulation is the idea of bundling data and methods that operate on that data within a single unit, while abstraction is the idea of hiding the implementation details of an object from the outside world.

Exam tips

  • Tip 1: Make sure to understand the concept of object-oriented programming (OOP) and how it is implemented in Python.
  • Tip 2: Practice creating Python classes and objects, and use inheritance, polymorphism, and encapsulation to solve problems.
  • Tip 3: Pay attention to the details of the problem and make sure to implement the correct attributes and methods.
  • Tip 4: Use the self parameter to refer to the current instance of the class, and use the super() function to access the attributes and methods of the superclass.
  • Tip 5: Use private attributes and methods to encapsulate the data and behavior of the object.

MCQs with explanations

MCQ 1

[F] What is the purpose of a class in object-oriented programming?

A) To create a new object B) To define a new type of object C) To inherit attributes and methods from another class D) To encapsulate data and behavior

Correct answer: B) To define a new type of object Why the distractors fail: A) Creating a new object is the purpose of instantiation, not a class. C) Inheritance is a mechanism that allows one class to inherit attributes and methods from another class, but it is not the purpose of a class. D) Encapsulation is the idea of bundling data and methods that operate on that data within a single unit, but it is not the purpose of a class.

MCQ 2

[H] What is the difference between method overriding and method overloading?

A) Method overriding occurs when a subclass provides a different implementation of a method that is already defined in its superclass, while method overloading occurs when multiple methods with the same name but different parameters are defined. B) Method overriding occurs when multiple methods with the same name but different parameters are defined, while method overloading occurs when a subclass provides a different implementation of a method that is already defined in its superclass. C) Method overriding is used for inheritance, while method overloading is used for polymorphism. D) Method overriding is used for polymorphism, while method overloading is used for inheritance.

Correct answer: A) Method overriding occurs when a subclass provides a different implementation of a method that is already defined in its superclass, while method overloading occurs when multiple methods with the same name but different parameters are defined. Why the distractors fail: B) This is the opposite of the correct answer. C) Method overriding is used for inheritance, but method overloading is not used for polymorphism. D) Method overriding is used for polymorphism, but method overloading is not used for inheritance.

MCQ 3

[F] What is the purpose of encapsulation in object-oriented programming?

A) To hide the implementation details of an object from the outside world B) To bundle data and methods that operate on that data within a single unit C) To inherit attributes and methods from another class D) To create a new object

Correct answer: B) To bundle data and methods that operate on that data within a single unit Why the distractors fail: A) Encapsulation is related to abstraction, but it is not the same thing. C) Inheritance is a mechanism that allows one class to inherit attributes and methods from another class, but it is not the purpose of encapsulation. D) Creating a new object is the purpose of instantiation, not encapsulation.

MCQ 4

[H] What is the difference between a class and an object?

A) A class is a template for creating objects, while an object is an instance of a class. B) A class is an instance of a class, while an object is a template for creating objects. C) A class is a type of object, while an object is a type of class. D) A class is a subclass of an object, while an object is a superclass of a class.

Correct answer: A) A class is a template for creating objects, while an object is an instance of a class. Why the distractors fail: B) This is the opposite of the correct answer. C) A class is not a type of object, and an object is not a type of class. D) A class is not a subclass of an object, and an object is not a superclass of a class.

MCQ 5

[F] What is the purpose of the self parameter in a Python class?

A) To refer to the current instance of the class B) To refer to the superclass of the class C) To create a new object D) To encapsulate data and behavior

Correct answer: A) To refer to the current instance of the class Why the distractors fail: B) The self parameter is not used to refer to the superclass of the class. C) Creating a new object is the purpose of instantiation, not the self parameter. D) Encapsulation is the idea of bundling data and methods that operate on that data within a single unit, but it is not the purpose of the self parameter.

Short-answer questions

Question 1

Explain the concept of object-oriented programming (OOP) and how it is implemented in Python.

Answer: Object-oriented programming (OOP) is a paradigm that organizes software design around objects and their interactions. In Python, OOP is implemented using classes and objects. A class is a template for creating objects, while an object is an instance of a class. Classes can have attributes, which are data members that describe the object, and methods, which are functions that operate on the object. Inheritance is a mechanism that allows one class to inherit the attributes and methods of another class. Polymorphism is the ability of an object to take on multiple forms. Encapsulation is the idea of bundling data and methods that operate on that data within a single unit.

Question 2

Explain the difference between method overriding and method overloading.

Answer: Method overriding occurs when a subclass provides a different implementation of a method that is already defined in its superclass. Method overloading occurs when multiple methods with the same name but different parameters are defined.

Question 3

Explain the purpose of encapsulation in object-oriented programming.

Answer: Encapsulation is the idea of bundling data and methods that operate on that data within a single unit. This helps to hide the implementation details of an object from the outside world and makes it easier to modify the object without affecting other parts of the program.

Question 4

Explain the difference between a class and an object.

Answer: A class is a template for creating objects, while an object is an instance of a class. A class defines the attributes and methods of an object, while an object is a specific instance of a class with its own set of attributes and methods.

Question 5

Explain the purpose of the self parameter in a Python class.

Answer: The self parameter is used to refer to the current instance of the class. It is a reference to the object itself and is used to access the attributes and methods of the object.