You are on page 1of 22

AT THE END OF THE

LESSON THE LEARNERS


ARE EXPECTED TO:

Describe • Describe polymorphism in Java

• Explain how to achieve


Explain polymorphism in Java

• Develop a Java program that


Develop demonstrates the use of
polymorphism
PICTURE ANALYSIS
Answer the following questions:
1. What can you observe in the
above figure?
2. How many roles does a man
have?
3. If a man is only one but can take
Man
multiple roles like a dad, an
employee a salesperson etc., what
do you think is this ability?
POLYMORPHISM IN
JAVA
Polymorphism comes from the Greek word Poly
means “MANY” and morph means “MANY
FORMS”.

POLYMORPHI It is an OOP technique that utilizes inheritance to


create 1 class and make several classes inherit
SM from that class so that it can take many forms.

To put it simply, polymorphism in Java allows us


to perform the same action in many different
ways.
REAL LIFE
EXAMPLE OF
POLYMORPHISM

Suppose you want to write a function to


save two contact numbers of the same
person, you can create it like –

void createContact(String name, int


number1, int number2).
USING POLYMORPHISM
class Dog extends Animal {
class Animal { void makeSound(){
void makeSound(){ //Arf
//Sound }
}
} class Cat extends Animal {
} void makeSound(){
//Super Class //Meow
}
}
//Subclasses
Inheritance lets us inherit attributes and methods from another class.
Polymorphism uses those methods to perform different tasks. This
allows us to perform a single action in different ways.
WE CAN ACHIEVE POLYMORPHISM IN JAVA
USING THE FOLLOWING WAYS:
During inheritance in Java, if the same method is
present in both the superclass and the subclass. Then,
the method in the subclass overrides the same method
in the superclass. This is called method overriding.
METHOD In this case, the same method will perform one
OVERRIDING operation in the superclass and another operation in
the subclass.

Note: The method that is called is determined during


the execution of the program. Hence, method
overriding is a run-time polymorphism.
JAVA METHOD OVERLOADING

In a Java class, we can create methods with the same name if they
differ in parameters.

Note: The method that is called is determined by the compiler.


Hence, it is also known as compile-time polymorphism.
DEMO TIME
The teacher will demonstrate how to develop a java program
showing polymorphism
IMPORTANCE OF POLYMORPHISM
It helps the programmer to reuse the codes, i.e., classes once written,
tested and implemented can be reused as required. Saves a lot of time.

Single variable can be used to store multiple data types.

Easy to debug the codes.


LET’S TRY!
Directions: Determine the output of the examples program in java. Then
identify if it is method overloading or method overriding.
1.
LET’S TRY!
Directions: Determine the output of the examples program in java. Then
identify if it is method overloading or method overriding.
2.
LET’S TRY!
Directions: Determine the output of the examples program in java. Then
identify if it is method overloading or method overriding.
1.
Output:
bowl

Method
Overriding
LET’S TRY!
Directions: Determine the output of the examples program in java. Then
identify if it is method overloading or method overriding.
2. Output:
woof
woof
woof

Method
Overloading
HANDS-ON
ACTIVITY
IT’S YOUR TIME TO
SHINE!
The programmer, the dancer, and the
singer are types of Person that would be
represented by subclasses of the Person
class. A person class has the methods
learn(), walk(), and eat(). Include
attributes such as Name and designation.
Construct a Java program that will show
polymorphism for the sample scenario.
IT’S YOUR TIME TO
SHINE!
Consider the following example, Shape is
super class for Rectangle and Triangle
class. Construct a Java program that will
show polymorphism for the sample
scenario.
Rubrics for Creating a Java program
WHAT HAVE YOU
LEARNED?

1. What is
polymorphism in Java?
2. How will you
achieve polymorphism
in Java?
WHAT HAVE YOU
LEARNED?

1. What do you think is


importance of polymorphism?

2. Do you think this lesson is


useful in your day to day living
experiences?
THANK YOU 

You might also like