Programming Polymorphism (OCR A Level Computer Science)

Revision Note

Robert Hampton

Written by: Robert Hampton

Reviewed by: James Woodhouse

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);
    }
}

Last updated:

You've read 0 of your 5 free revision notes this week

Sign up now. It’s free!

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

Expertise: Computer Science Content Creator

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.

James Woodhouse

Author: James Woodhouse

Expertise: Computer Science

James graduated from the University of Sunderland with a degree in ICT and Computing education. He has over 14 years of experience both teaching and leading in Computer Science, specialising in teaching GCSE and A-level. James has held various leadership roles, including Head of Computer Science and coordinator positions for Key Stage 3 and Key Stage 4. James has a keen interest in networking security and technologies aimed at preventing security breaches.