Programming Polymorphism (OCR A Level Computer Science)

Revision Note

Robert Hampton

Expertise

Computer Science Content Creator

Programming Polymorphism

  • To program polymorphism, you should already have a solid understanding of what polymorphism in Object Oriented Programming (OOP) is.

How do you Define Polymorphism?

Pseudocode 

1. Create a blueprint for a general animal:

  • Define a template called Animal

  • Give Animal a placeholder action called speak() that does nothing yet

2. Create a blueprint for a dog:

  • Define a template called Dog based on the Animal template

  • Override the speak() action for Dog to make it print "Woof"

3. Create a blueprint for a cat:

  • Define a template called Cat based on the Animal template

  • Override the speak() action for Cat to make it print "Meow"

4. Create a function to make animals speak:

  • Define a function called make_sound(animal)

  • Inside the function, tell the given animal to perform its speak() action

5. Create a dog and a cat:

  • Build a Dog object and put it in a box labeled "dog"

  • Build a Cat object and put it in a box labeled "cat"

6. Ask the dog and cat to speak:

  • Call the make_sound() function with the dog object

  • Call the make_sound() function with the cat object

Python

class Animal:
    def speak(self):
        pass

class Dog(Animal):
    def speak(self):
        print("Woof")

class Cat(Animal):
    def speak(self):
        print("Meow")

def make_sound(animal):
    # calling the speak method of the Animal class
    animal.speak()

dog = Dog()
cat = Cat()

make_sound(dog)
make_sound(cat)

Java

class Animal {
    public void speak() {
        // Empty default implementation
    }
}

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);
        makeSound(cat);
    }
}

You've read 0 of your 0 free revision notes

Get unlimited access

to absolutely everything:

  • Downloadable PDFs
  • Unlimited Revision Notes
  • Topic Questions
  • Past Papers
  • Model Answers
  • Videos (Maths and Science)

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Did this page help you?

Robert Hampton

Author: Robert Hampton

Rob has over 16 years' experience teaching Computer Science and ICT at KS3 & GCSE levels. Rob has demonstrated strong leadership as Head of Department since 2012 and previously supported teacher development as a Specialist Leader of Education, empowering departments to excel in Computer Science. Beyond his tech expertise, Robert embraces the virtual world as an avid gamer, conquering digital battlefields when he's not coding.