What Is Polymorphism in Java Notes
What Is Polymorphism in Java Notes
Polymorphism in Java occurs when there are one or more classes or objects
related to each other by inheritance. It is the ability of an object to take many
forms. Inheritance lets users inherit attributes and methods, and polymorphism
uses these methods to perform different tasks. So, the goal is communication, but
the approach is different.
For example, you have a smartphone for communication. The communication
mode you choose could be anything. It can be a call, a text message, a picture
message, mail, etc. So, the goal is common that is communication, but their
approach is different. This is called Polymorphism.
The word polymorphism means having many forms. In simple words, we can
define polymorphism as the ability of a message to be displayed in more
than one form.
Real life example of polymorphism: A person at the same time can have
different characteristic. Like a man at the same time is a father, a husband,
an employee. So the same person posses different behavior in different
situations. This is called polymorphism.
class Shapes {
class Main {
myShape.area();
myTriangle.area();
myShape.area();
myCircle.area();
}
Types of Polymorphism
Polymorphism in Java can be performed by two different methods:
1. Method Overloading
2. Method Overriding
Method overloading is defined as a process that can create multiple methods of the same
name in the same class, and all the methods work in different ways. Method overloading
occurs when there is more than one method of the same name in the class.
2
3
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Shapes {
}
public void area(int r) {
System.out.println("Triangle area="+0.5*b*h);
System.out.println("Rectangle area="+l*b);
class Main {
myShape.area();
myShape.area(5);
myShape.area(6.0,1.2);
myShape.area(6,2);
Method overriding is defined as a process when the subclass or a child class has the same
method as declared in the parent class.
class Vehicle{
//defining a method
obj.run();//calling method
Method Overloading is when a class has multiple methods with the same name, but the
number, types and order of parameters and the return type of the methods are different.
Java allows the user freedom to use the same name for various functions as long as it can
distinguish between them by the type and number of parameters.
Example of Compile- Time Polymorphism in java
package staticPolymorphism;
int c = a+b;
int c = a+b+e;
obj.sum ( 30,90);
}
The output of the program will be:
Sum of two numbers: 120
In this program, the sum() method was overloaded with two types with different
parameters.
This is the basic concept of compile-time polymorphism in java where we can perform
various operations by using multiple methods having the same name.
Method Overriding is done when a child or a subclass has a method with the same name,
parameters and return type as the parent or the superclass, then that function overrides the
function in the superclass. In simpler terms, if the subclass provides its definition to a
method already present in the superclass, then that function in the base class is said to be
overridden.
It should be noted that runtime polymorphism can only be achieved through functions and
not data members.
Upcasting is done when the Parent class’s reference variable refers to the object of the
child class. For example:
1 class A{}
2 class B extends A{}
Example 1:
In this example, we are creating one superclass Animal and three subclasses, Herbivores,
Carnivores and Omnivores. Subclasses extend the superclass and override its eat()
method. We will call the eat() method by the reference variable of Parent class, i.e.
Animal class. As it refers to the base class object and the base class method overrides the
superclass method, the base class method is invoked at runtime. As Java Virtual Machine
or the JVM and not the compiler determines method invocation, it is runtime
polymorphism.
class Animal{
void eat(){
System.out.println("Animals Eat");
void eat(){
void eat(){
void eat(){
System.out.println("Carnivores Eat meat");
class main{
A.eat();
h.eat();
o.eat();
c.eat();
ADVANTAGES OF POLYMORPHISM
It helps programmers reuse the code and classes once written, tested and implemented. They
can be reused in many ways.
Single variable name can be used to store variables of multiple data types(Float, double,
Long, Int etc).
Polymorphism helps in reducing the coupling between different functionalities.
DISADVANTAGES OF POLYMORPHISM
One of the disadvantages of polymorphism is that developers find it difficult to implement
polymorphism in codes.
Run time polymorphism can lead to the performance issue as machine needs to decide which
method or variable to invoke so it basically degrades the performances as decisions are taken at run
time.
Polymorphism reduces the readability of the program. One needs to identify the runtime
behavior of the program to identify actual execution time.