Polymorphism
Polymorphism is derived from 2 greek words 'poly' and 'morphs'. The word poly means many and morphs mean behavior or form.
Poly + Morphs
↓ ↓
Polymorphism in java is an oops concept, Whenever an object producing different-different behavior in different-different circumstances is called polymorphism in java.
many behavior or form = many behavior
Polymorphism is always achieved via behavior(member function) of an object. Properties(data member) of an object do not play any role in case of polymorphism. Polymorphism can be defined in other words "one name many forms".
Examples of polymorphism related to real life
(1) Related to person- Suppose you are in a classroom that time you behave like a student.
- When you at your home at that time you behave like a son or daughter or brother.
- When you are in a market, you behave like a customer.
(2) Related to product
- Suppose Air Conditioner producing hot air in winter and cold air in summer.
Types of polymorphism in java, these are
- Compile time or static polymorphism
- Run time or dynamic polymorphism
Compile-time polymorphism
Method overloading (function overloading) and operator overloading both are compile time polymorphism. Method overloading(function overloading) or static binding(early binding ) is also known as compile time polymorphism in java.
"Whenever an object is bound with their functionality at compile time, this known as compile time polymorphism".
👉: Java does not support compile-time or static polymorphism. Java supports only dynamic polymorphism or run-time polymorphism.
Runtime polymorphism
Method overriding (function overriding) or Dynamic Binding(late binding) is known as runtime polymorphism in java. In java polymorphism principal is implemented with method overriding concept."Whenever an object is bound with their functionality at run-time, this known as run time polymorphism".
👉: Without inheritance concept method overriding is not possible in java So we will learn method overriding or dynamic binding after inheritance concept of oops.
Difference between Compile Time and Run Time Polymorphism in Java
Compile-Time Polymorphism
- In Compile Time polymorphism, call is resolved by the compiler.
- Compile-Time polymorphism is also known as static binding, early binding, and overloading as well.
- Overloading is compile time polymorphism, where more than one methods share the same name with different parameter or arguments.
- Compile Time polymorphism is achieved by function overloading or method overloading and operator overloading.
- It provides fast execution because known early at compile time.
- Compile time polymorphism is less flexible as all thing executes at compile time.
Run-Time Polymorphism
- In Run-Time polymorphism, call is not resolved by the compiler.
- Run-Time polymorphism is also known as dynamic binding, late binding, and overriding as well.
- Overriding is runtime polymorphism having the same method with same parameters or arguments but associated in a class and its subclass.
- It is achieved by method overriding.
- It provides slow execution as compared to early binding because it is known as run time.
- Runtime polymorphism is more flexible because all thing executes at runtime.
Read More: