Polymorphism

Computer Science: OCR A Level, topic 2.4.12

Polymorphism

In object-oriented programming, polymorphism is when a subclass alters its inherited methods in two ways: by overloading or overriding.

Overloading

Overloading

  • Overloading is when a method is defined in the subclass that has the same name, but different arguments to a method defined in the superclass.
  • The method might also have a different return type.
  • For example, a shape superclass might have an area calculating function.
  • The subclass circle could have the function area(radius), whereas a triangle subclass could have the function area(base,height).
Overriding

Overriding

  • Overriding is when a method is redefined in the subclass with the same name and same arguments.
  • The method that is defined in the subclass overrides the method defined in the super class.

Test yourself on Polymorphism

  • What is the term used for when a subclass alters its inherited methods?

  • Which property must be the same when overloading a method in a subclass?

Practise this topic for free