Lesson 7 - Polymorphism
Lesson 7 - Polymorphism
Polymorphism
• Introduction to Polymorphism
• Method Overloading
• Method Overriding
• Constructor Overloading
• Polymorphic Behavior of Objects
Polymorphism Definition: It means ‘Different Forms of the Same Thing’. In other words ‘One
Name, Different Forms’.
Example:
✓ Imagine, you and your friend are walking inside your MidTerm exam room. Just before
entering the room, your friend is saying, “You are my best friend, brother. Don’t worry
about the exam. I got your back. I’ll slide my script a bit right from me, all you need to do
is to take a peek and write. ”Now, during the exam, no matter how much you poke your
friend, your friend is neither responding nor sliding the script. You are really upset
with your friend. And after the exam, your friend is like, “I’m sorry, brother. Please forgive
me. I’m your best friend. Let me give you a treat. Lets have some fun in Canteen.”
In Programming the polymorphic behavior might be with methods, Constructors and Objects.
• Method Overloading.
• Method Overriding.
• Constructor Overloading.
• Polymorphic Behavior of Objects.
Method Overloading:
As the name suggests, it happens among methods. There are some conditions of method
overloading. If and only If these conditions are met, we can say that there are method
overloading among those methods.
These conditions are:
Here all add methods are overloaded methods. All of the conditions for overloading is fulfilled.
Method Overriding: Again, it happens among methods. There are some conditions of method
overriding. If and only If these conditions are met, we can say that there are method overriding
among those methods.
These conditions are:
Constructor Overloading: As the name suggests, it happens among Constructors. There are
some conditions of constructor overloading. If and only If these conditions are met, we can say
that there is constructor overloading in that class. These conditions are:
Rectangle()
Rectangle(double length,double width)
double area()
Circle Triangle
Start
Create object array of shape class. Store object of child classes in different index. Now invoke area
method of the clild classes using the object of shape class.