Polymorphism in Java

Safalta Expert Published by: Saksham Chauhan Updated Wed, 14 Sep 2022 12:58 AM IST

Highlights

Check Polymorphism in Java Here At Safalta.com
 

Polymorphism refers to the existence of several forms. Polymorphism may be simply defined as a message's capacity to be presented in several forms.
Download these FREE Ebooks:
1. Introduction to Digital Marketing
2. Website Planning and Creation

 

Real-world Example: Polymorphism

Different traits might exist in the same individual at the same time. A father, a spouse, and an employee are all dual roles that a man plays.

Source: Safalta.com

Therefore, the same person exhibits diverse behaviour depending on the circumstance. Polymorphism is the term for this. One of the key components of object-oriented programming is polymorphism.

Free Demo Classes

Register here for Free Demo Classes

Please fill the name
Please enter only 10 digit mobile number
Please select course
Please fill the email
We are able to carry out a single activity in several ways because to polymorphism. To put it another way, polymorphism enables the definition of a single interface with various implementations. It implies numerous forms since "poly" means many and "morphs" means forms.

Polymorphism types

  • Compile-time Polymorphism
  • Runtime Polymorphism

Compile-time polymorphism

Static polymorphism is another name for it. In order to create this form of polymorphism, either operators or functions must be overloaded.

Method Overloading: These functions are referred to as being overloaded when they have many instances of the same name but distinct arguments. A change in the number of arguments or/and a change in the type of arguments might cause a function to be overloaded.

Example:


Output: 
8
34.65

 Runtime polymorphism

Additionally called Dynamic Method Dispatch. A function call to the overridden method is resolved during this procedure at runtime. Method Overriding is used to accomplish this kind of polymorphism. Contrarily, method overriding happens when a derived class defines one of the base class's member functions. It is referred to as overriding that basic function.

Example:



Output:
 
subclass1
subclass2
 

 

Free E Books