Polymorphism (OOP) (OCR A Level Computer Science)

Revision Note

Jamie Wood

Expertise

Maths

Polymorphism (OOP)

What is Polymorphism?

  • 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

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

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

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?

Jamie Wood

Author: Jamie Wood

Jamie graduated in 2014 from the University of Bristol with a degree in Electronic and Communications Engineering. He has worked as a teacher for 8 years, in secondary schools and in further education; teaching GCSE and A Level. He is passionate about helping students fulfil their potential through easy-to-use resources and high-quality questions and solutions.