100% found this document useful (2 votes)
920 views11 pages

What Is Polymorphism in Java Notes

Polymorphism in Java allows objects of different classes to be treated as objects of their parent class. It occurs when classes inherit from a common parent class and define their own implementation of a method defined in the parent class. This allows a single method call to behave differently depending on the object it is called on. There are two types of polymorphism in Java - compile-time polymorphism through method overloading, and runtime polymorphism through method overriding. Runtime polymorphism is considered true polymorphism since the implementation is selected at runtime based on the actual object type.

Uploaded by

geethanjali
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
100% found this document useful (2 votes)
920 views11 pages

What Is Polymorphism in Java Notes

Polymorphism in Java allows objects of different classes to be treated as objects of their parent class. It occurs when classes inherit from a common parent class and define their own implementation of a method defined in the parent class. This allows a single method call to behave differently depending on the object it is called on. There are two types of polymorphism in Java - compile-time polymorphism through method overloading, and runtime polymorphism through method overriding. Runtime polymorphism is considered true polymorphism since the implementation is selected at runtime based on the actual object type.

Uploaded by

geethanjali
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 11

What is Polymorphism in Java?

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.

Why use Polymorphism in Java?


Polymorphism in Java makes it possible to write a method that can correctly process lots
of different types of functionalities that have the same name. We can also gain
consistency in our code by using polymorphism.

Advantages of Polymorphism in Java


1. It provides reusability to the code. The classes that are written, tested
and implemented can be reused multiple times. This, in turn, saves a
lot of time for the coder. Also, the code can be changed without
affecting the original code.
2. A single variable can be used to store multiple data values. The value
of a variable inherited from the superclass into the subclass can be
changed without changing that variable’s value in the superclass or any
other subclasses.
3. With lesser lines of code, it becomes easier for the programmer to
debug the code.

class Shapes {

public void area() {

System.out.println("The formula for area of ");

class Triangle extends Shapes {

public void area() {

System.out.println("Triangle is ½ * base * height ");

class Circle extends Shapes {

public void area() {

System.out.println("Circle is 3.14 * radius * radius ");

class Main {

public static void main(String[] args) {

Shapes myShape = new Shapes(); // Create a Shapes object

Shapes myTriangle = new Triangle(); // Create a Triangle object

Shapes myCircle = new Circle(); // Create a Circle object

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

What is Method Overloading in Java?

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.

Example of Method Overloading in Java

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() {

System.out.println("Find area ");

}
public void area(int r) {

System.out.println("Circle area = "+3.14*r*r);

public void area(double b, double h) {

System.out.println("Triangle area="+0.5*b*h);

public void area(int l, int b) {

System.out.println("Rectangle area="+l*b);

class Main {

public static void main(String[] args) {

Shapes myShape = new Shapes(); // Create a Shapes object

myShape.area();

myShape.area(5);

myShape.area(6.0,1.2);

myShape.area(6,2);

What is Method Overriding in Java?

Method overriding is defined as a process when the subclass or a child class has the same
method as declared in the parent class.

Example of Method Overriding in Java

class Vehicle{

//defining a method

void run(){System.out.println("Vehicle is moving");}


}

//Creating a child class

class Car2 extends Vehicle{

//defining the same method as in the parent class

void run(){System.out.println("car is running safely");}

public static void main(String args[]){

Car2 obj = new Car2();//creating object

obj.run();//calling method

Polymorphism in java can be classified into two types:

1. Static / Compile-Time Polymorphism


2. Dynamic / Runtime Polymorphism

What is Compile-Time Polymorphism in Java?

Compile-Time polymorphism in java is also known as Static Polymorphism. In this


process, the call to the method is resolved at compile-time. Compile-Time polymorphism
is achieved through Method Overloading. This type of polymorphism can also be
achieved through Operator Overloading. However, Java does not support Operator
Overloading.

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

We will do addition in java and understand the concept of compile-time polymorphism


using subtract() 

package staticPolymorphism;

public class Addition

void sum(int a, int b)

int c = a+b;

System.out.println(“ Addition of two numbers :” +c); }

void sum(int a, int b, int e)

int c = a+b+e;

System.out.println(“ Addition of three numbers :” +c); }

public static void main(String[] args)

Addition obj = new Addition();

obj.sum ( 30,90);

obj.sum(45, 80, 22);

}
The output of the program will be: 
Sum of two numbers: 120 

Sum of three numbers: 147 

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.

What is Runtime Polymorphism in Java?

Runtime polymorphism in java is also known as Dynamic Binding or Dynamic


Method Dispatch. In this process, the call to an overridden method is resolved
dynamically at runtime rather than at compile-time. Runtime polymorphism is achieved
through Method Overriding.

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. 

Overriding is done by using a reference variable of the superclass. Which method to be


called is determined based on the object which is being referred to by the reference
variable. This is also known as Upcasting.

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{} 

3 A a=new B(); //upcasting

Examples of Runtime Polymorphism  in Java

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");

class herbivores extends Animal{

void eat(){

System.out.println("Herbivores Eat Plants");

class omnivores extends Animal{

void eat(){

System.out.println("Omnivores Eat Plants and meat");

class carnivores extends Animal{

void eat(){
System.out.println("Carnivores Eat meat");

class main{

public static void main(String args[]){

Animal A = new Animal();

Animal h = new herbivores(); //upcasting

Animal o = new omnivores(); //upcasting

Animal c = new carnivores(); //upcasting

A.eat();

h.eat();

o.eat();

c.eat();

IMPORTANT POINTS ABOUT POLYMORPHISM


  A functionality can behave differently for different instances
 The behavior depends on the type of data used in the operation
 Polymorphism is used for implementing inheritance.

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.

You might also like