All IT Courses 50% Off
JAVA Tutorials

Introduction to Polymorphism

Polymorphism means one name can have many forms.

To understand this, let us take an example, suppose you have a smartphone for communication purposes. The communication mode may vary. It can be anything that may be a call, a text message, a picture message, mail, etc. But the goal is the same in all communication modes, that is, communication. This is called Polymorphism, where the purpose is the same but with different approaches. An example of polymorphism is Method Overriding.

Method Overriding

Method Overriding is used to define a superclass method in a subclass.

Some basic rules for Method Overriding

  • The signature of the method, i.e., method name, parameter list, and return type should be the same
  • If a method present in the superclass is defined as private, then the subclass can make it public but not vice versa.

What is Dynamic Polymorphism?

Dynamic Polymorphism is the process in which multiple methods are defined with the same name and signature in both superclass and subclass. 

All IT Courses 50% Off

Example of Dynamic Polymorphism:

Doctor obj = new Surgeon();
Consider the statement
obj.treatPatient();

Here the variable “obj” refers to the object of the parent class, but the object is pointing to the method of the child class.

Introduction to Polymorphism

obj.treatPatient() will then execute treatPatient() method of the subclass named Surgeon

If we use a subclass reference to call a method, then the method that is going to be invoked will be decided by the JVM.

For example, obj here is a reference object to Doctor, it will call the method of Surgeon, as obj here points to a Surgeon object

All these things are decided during run-time and hence called as dynamic or run-time polymorphism.

But what if the treatPatient method in the Surgeon class wants to access the functionality defined in Doctor class and then want to perform its specific functionality?

This can be achieved using the super keyword to access methods of the superclass from the subclass.

The treatPatient method in the Surgeon class will then be written as:

treatPatient(){
   super.treatPatient();
     //add code specific to Surgeon

The complete code will be :

Introduction to Polymorphism

Introduction to Polymorphism

Difference between Static & Dynamic Polymorphism

Static Polymorphism

Dynamic Polymorphism

Related to method overloading. Related to method overriding.
Errors get resolved at compile time only. Errors get resolved at run time.
void sum (int a , int b);
void sum (float a, double b);
int sum (int a, int b); //compiler gives error.
//reference of parent pointing to child object
Doctor obj = new Surgeon();
// method of child called
obj.treatPatient();

 

Facebook Comments

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.

Related Articles

Back to top button