Want to create interactive content? It’s easy in Genially!

Get started free

Polymorphism

Adam Wingell

Created on March 11, 2025

Start designing with a free template

Discover more than 1500 professional designs like these:

Transcript

This class derives from the base class and has automatic access to the MakeSound() method. It then defines its own method MakeSound() which prints to console "Bark!." This through the keyword override and the method MakeSound() overrides the parent classes method and prints its own method MakeSound() instead.

The main program starts by creating a variable of the Animal class called myAnimal, then uses polymorphism to create an instance of the Dog class, this invokes the overriding method from the Dog class.

The resulting output would be as follows: Bark!

The Base class contains contains a method within it called MakeSound() which prints to console "Some generic animal sound." The mothod has the keyword virtual within it which allows other methods within its derived classes to overide the method with its own methods of the exact same name.

Base class

derived class

Resulting output

main program

Polymorphism

Here we have an example of Polymorphism or method overriding, this is where a child class overrides the method of the parent class. The parent class must have the keyword virtual in its method and the child method must have the keyword override in its method for polymorphism to work. Click through the options below.