Computer-science_A-level_Cie
-
computers-and-components6 主题
-
logic-gates-and-logic-circuits2 主题
-
central-processing-unit-cpu-architecture6 主题
-
assembly-language-4 主题
-
bit-manipulation1 主题
-
operating-systems3 主题
-
language-translators2 主题
-
data-security3 主题
-
data-integrity1 主题
-
ethics-and-ownership3 主题
-
database-concepts3 主题
-
database-management-systems-dbms-1 主题
-
data-definition-language-ddl-and-data-manipulation-language-dml1 主题
-
computational-thinking-skills1 主题
-
algorithms14 主题
-
data-types-and-records2 主题
-
arrays2 主题
-
files1 主题
-
introduction-to-abstract-data-types-adt1 主题
-
programming-basics1 主题
-
constructs2 主题
-
structured-programming1 主题
-
program-development-life-cycle2 主题
-
program-design-2 主题
-
program-testing-and-maintenance3 主题
-
user-defined-data-types1 主题
-
file-organisation-and-access-3 主题
-
floating-point-numbers-representation-and-manipulation3 主题
-
protocols2 主题
-
circuit-switching-packet-switching1 主题
-
processors-parallel-processing-and-virtual-machines5 主题
-
boolean-algebra-and-logic-circuits4 主题
-
purposes-of-an-operating-system-os3 主题
-
translation-software3 主题
-
encryption-encryption-protocols-and-digital-certificates3 主题
-
artificial-intelligence-ai4 主题
-
recursion1 主题
-
programming-paradigms4 主题
-
object-oriented-programming7 主题
-
file-processing-and-exception-handling2 主题
-
data-representation5 主题
-
multimedia3 主题
-
compression2 主题
-
networks-and-the-internet11 主题
polymorphism-oop
Polymorphism (OOP)
What is polymorphism?
-
In A Level Computer Science, polymorphism is a concept in programming that allows objects to take on different forms or behaviours
-
Different objects can share the same name or behaviour but can work in different ways
-
It helps make code more flexible, reusable, and easier to maintain
-
It allows flexibility and reusability in programming, making it easier to write and manage code
-
Objects can be treated as belonging to a common group, even if they belong to different classes, making your code more versatile and adaptable to changes
Example 1 – method overloading

Method Overloading Example 1
-
In the example above, all three classes all have a method named
move(). Polymorphism allows methods to be declared with the same name but execute different code (in this case printing different messages) -
The override keyword Is used to provide a new implementation for a method that is already defined in the parent class (base class)
Example 2 – method overloading

Method Overloading Example 2
-
In the above example both the Motorcycle class and the Car class both inherit from the base class ‘Cars’
-
Objects from the Motorcycle Class and the Car class can call the
startEngines()method which will output"Engines Started!" -
If either of the object types call the
displayInfo()method, the program will execute the method from the objects class as it overrides the Vehicle class method -
For example
-
If a motorcycle object calls the
displayInfo()method,"I am a Motorcycle!"will be output -
If a Car object calls the
displayInfo()method,"I am a Car!"will be output
-
Treating objects as common groups
-
Polymorphism also allows objects of different classes to be treated as objects of a common superclass or base class
-
For example:
-
Vehicle vehicle1 = new Car() -
Vehicle vehicle2 = new Motorcycle()
-
-
This allows an array of type Vehicle to store both Motorcycle and Car objects rather than in separate data structures
-
If the
vehicle1.displayInfo()method is called, it will still output “I am a Car!” -
If the
vehicle2.displayInfo()method is called, it will still output “I am a Motorcycle!”
-
-
This flexibility provided by polymorphism are essential for creating more maintainable and modular code
Programming polymorphism (OOP)
How do you program polymorphism?
Run-time polymorphism
-
This happens when a subclass overrides a method defined in a superclass, and the correct version is chosen at run-time, not compile-time
Example scenario: Animal, dog, and cat
Step-by-step logic (pseudocode)
-
Define a superclass called
Animalwith a methodspeak()that does nothing -
Create subclasses called
DogandCatthat inherit fromAnimal -
In each subclass, override the
speak()method to provide a specific output -
Write a function
make_sound(animal)that accepts an object of typeAnimaland calls itsspeak()method -
Create instances of
DogandCat, and pass them tomake_sound()
CLASS Animal METHOD speak() // Empty method (acts as a placeholder) CLASS Dog EXTENDS Animal METHOD speak() OUTPUT "Woof" CLASS Cat EXTENDS Animal METHOD speak() OUTPUT "Meow" PROCEDURE make_sound(animal : Animal) CALL animal.speak() // Create objects
DECLARE myDog : Dog
DECLARE myCat : Cat SET myDog TO NEW Dog()
SET myCat TO NEW Cat() // Demonstrate polymorphism
CALL make_sound(myDog) // Outputs: Woof
CALL make_sound(myCat) // Outputs: Meow
-
Animalis a base class with aspeak()method -
DogandCatare subclasses that override thespeak()method -
make_sound()takes anAnimalobject but calls the correct version ofspeak()depending on whether it’s aDogor aCat -
This demonstrates run-time polymorphism – the method call is resolved based on the actual type of the object
Python
class Animal: def speak(self): pass # Placeholder method class Dog(Animal): def speak(self): print("Woof") class Cat(Animal): def speak(self): print("Meow") def make_sound(animal): animal.speak() dog = Dog()
cat = Cat() make_sound(dog) # Outputs: Woof
make_sound(cat) # Outputs: Meow
-
This shows method overriding
-
The
make_sound()function relies on dynamic method binding
Java
class Animal { public void speak() { // Empty default method }
} class Dog extends Animal { @Override public void speak() { System.out.println("Woof"); }
} class Cat extends Animal { @Override public void speak() { System.out.println("Meow"); }
} public class Main { public static void makeSound(Animal animal) { animal.speak(); } public static void main(String[] args) { Dog dog = new Dog(); Cat cat = new Cat(); makeSound(dog); // Outputs: Woof makeSound(cat); // Outputs: Meow }
}
-
Recognise use of a superclass reference (
Animal animal) pointing to a subclass object -
Polymorphism occurs when
animal.speak()dynamically calls the correct overridden method
Worked Example
The child class Helicopter inherits from the parent class Vehicle. A helicopter also has a vertical position and changes the vertical position when it increases speed.

The Helicopter method IncreaseSpeed() overrides the method from the parent
class and:
-
adds the amount of vertical change to the vertical position
-
adds
IncreaseAmountto the current speed -
adds the updated current speed to the horizontal position.
The vertical position of a helicopter cannot exceed its maximum height.
The current speed of a helicopter cannot exceed its maximum speed.
Write program code for the method IncreaseSpeed() [4]
Answer
-
Method header (overriding where required) with no parameter [1 mark]
-
Adding vertical change to vertical position … [1 mark]
-
…limiting to maximum height [1 mark]
-
Repeating/calling/using the code from original for horizontal increase (in every case) [1 mark]
Example program code:
VB.NET
Overrides Sub IncreaseSpeed() VerticalPosition = VerticalPosition + VerticalChange If VerticalPosition > MaxHeight Then VerticalPosition = MaxHeight End If Me.SetCurrentSpeed(GetCurrentSpeed() + GetIncreaseAmount()) If Me.GetCurrentSpeed() > Me.GetMaxSpeed() Then Me.SetCurrentSpeed(Me.GetMaxSpeed()) End If Me.SetHorizontalPosition(Me.GetHorizontalPosition() + Me.GetCurrentSpeed())
End Sub
Java
public void IncreaseSpeed(){ VerticalPosition = VerticalPosition + VerticalChange; if(VerticalPosition > MaxHeight){ VerticalPosition = MaxHeight; } super.SetCurrentSpeed(super.GetCurrentSpeed() + super.GetIncreaseAmount()); if(super.GetCurrentSpeed() > super.GetMaxSpeed()){ super.SetCurrentSpeed(super.GetMaxSpeed()); } super.SetHorizontalPosition(super.GetHorizontalPosition() + super.GetCurrentSpeed());
}
Python
def IncreaseSpeed(self): self.__VerticalPosition = self.__VerticalPosition + self.__VerticalChange if(self.__VerticalPosition > self.__MaxHeight): self.__VerticalPosition = MaxHeight Vehicle.SetCurrentSpeed(self, Vehicle.GetCurrentSpeed(self) +
Vehicle.GetIncreaseAmount(self)) if(Vehicle.GetCurrentSpeed(self) > Vehicle.GetMaxSpeed(self)): Vehicle.SetCurrentSpeed(self, Vehicle.GetMaxSpeed(self));
Vehicle.SetHorizontalPosition(self, Vehicle.GetHorizontalPosition(self) +
Vehicle.GetCurrentSpeed(self))
Responses