Poly means ---> many.
morphism means--> forms.
Simply polymorphism means one name used for multiple purpose.
There are 2 types of polymorphisms in Java.
For example add(int a, int b) is a method, now I will create 2 more methods add(float a, float b) and add(double a, double b).
Even though the method name is same for all these methods, it will perform different task based on parameters we are passing.
add(int a,int b) ---> for adding 2 integers.
add(float a, float b)---> for adding 2 float numbers
add(double a, double b)--> for adding 2 double values.
purpose is same for all the above methods, but task is different(adding different types)
1. compile time polymorphism (method overloading is example).
All the above methods are said to be overloaded methods. at the compilation time it will be identify all the methods.
method overloading is done in the same class.
2. runtime polymorphism (method overriding is example)
method overriding is nothing but re-writing of the method in the child class.
method overriding is done in parent-child class.
If we are not satisfied with the method implementation in the super class, want to provide our own implementation in child class of the method then we will re-write the method definition/ implementation in the child class.
JVM will take care of calling the method at runtime so method overriding is comes under runtime polymorphism.
morphism means--> forms.
Simply polymorphism means one name used for multiple purpose.
There are 2 types of polymorphisms in Java.
For example add(int a, int b) is a method, now I will create 2 more methods add(float a, float b) and add(double a, double b).
Even though the method name is same for all these methods, it will perform different task based on parameters we are passing.
add(int a,int b) ---> for adding 2 integers.
add(float a, float b)---> for adding 2 float numbers
add(double a, double b)--> for adding 2 double values.
purpose is same for all the above methods, but task is different(adding different types)
1. compile time polymorphism (method overloading is example).
All the above methods are said to be overloaded methods. at the compilation time it will be identify all the methods.
method overloading is done in the same class.
2. runtime polymorphism (method overriding is example)
method overriding is nothing but re-writing of the method in the child class.
method overriding is done in parent-child class.
If we are not satisfied with the method implementation in the super class, want to provide our own implementation in child class of the method then we will re-write the method definition/ implementation in the child class.
JVM will take care of calling the method at runtime so method overriding is comes under runtime polymorphism.