Test Topic 3: Polymorphism

Polymorphism is the concept where an object behaves differently in different situations. There are two types of polymorphism – compile time polymorphism and runtime polymorphism.

Compile-time polymorphism is achieved by method overloading. For example, we can have a class as below.

Runtime polymorphism is implemented when we have “IS-A” relationship between objects. This is also called a method overriding because subclass has to override the superclass method for runtime polymorphism.

If we are working in terms of the superclass, the actual implementation class is decided at runtime. The compiler is not able to decide which class method will be invoked. This decision is done at runtime, hence the name as runtime polymorphism or dynamic method dispatch.