Polymorphism and Dynamic Method Lookup in Java

Polymorphism and dynamic method lookup are a powerful programming paradigm that simplifies client definitions, encourages object decoupling, and supports dynamically changing relationships between objects at runtime.

According to Liang, Ploymorphism is : “An object of a subtype can be used wherever its supertype value is required. This feature is known as polymorphism

Polymorphism and dynamic binding example

Polymorphism and dynamic binding example

In above example method m(Object x)  takes a parameter of object type. You can invoke it with any type of object because Object class is by default a super class and it can handle its subtype objects.  In example we have 3 classes Person, Student1, and GraduateStudent, where as x can be instance of any given class and every class have its own toString method which is overrieded. Which implementation will be used is determined by JVM at runtime. This capability is known as dynamic binding.

Comments

  1. By smith

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.