Polymorphism
Polymorphism
is the concept where an object behaves differently in different situationsSince the object takes multiple forms, it is called Polymorphism.
Types of Polymorphism
Static Polymorphism (compile time polymorphism/ Method overloading):
In a class,if we have multiple methods with same name but different in parameters then it is called Method Overloading
Method overloading
.
Whenever we need to perform similar operation but by using different number of parameters or different type of parameters then using same name for a method will increase the readability of the code.
Example
If we want to perform the addition of numbers using 2 parameters and 3 parameters then we can write 2 separate methods with the same name but with different number of parameters.
- Class Addition{
- Public int add(int x,int y){
- Return x+y;
- }
- Public int add(int x,int y,int z){
- Return x+y+z;
- }
- }
Class Addition{ Public int add(int x,int y){ Return x+y; } Public int add(int x,int y,int z){ Return x+y+z; } }
Now whenever we need to call these methods , we can pass appropriate number of parameters and compiler
will be able to indentify the exact matching method based on the parameter.
- Class AdditionTest{
- public static void main(String[] args){
- Addition addObj = new Addition();
- System.out.println(addObj.add(10,20));
- System.out.println(addObj.add(10,20,30));
- }
- }
Class AdditionTest{ public static void main(String[] args){ Addition addObj = new Addition(); System.out.println(addObj.add(10,20)); System.out.println(addObj.add(10,20,30)); } }
Since the decision of identifying the right method is taken at compile time itself, its called
Compile time polymorphism
.
Dynamic Polymorphism (run time polymorphism/ Method Overriding)
Before understanding Dynamic polymorphism, we need to have clear understanding of method overriding, because Dynamic polymorphism has to be achieved only by using method overriding.
Method overriding:
If the subclass or a child class has the same method (both the methods have same name and signature) as defined in its parent or super class then its called Method overriding. Both methods should have same name and signature but differ in their implementation.
Method overriding
helps us to achieve Runtime polymorphism.
Method overriding Rules
1) There must be Inheritance or parent child relationship between the classes
2) Both the methods (in parent and child class) should have the same name
3) Both the methods (in parent and child class) should have the same signature
Example:
Vehicle.java
- public class Vehicle {
- public void start(){
- System.out.println("Vehicle start logic");
- }
- public void stop(){
- System.out.println("Vehicle stop logic");
- }
- }
public class Vehicle { public void start(){ System.out.println("Vehicle start logic"); } public void stop(){ System.out.println("Vehicle stop logic"); } }
TwoWheeler.java
- class TwoWheeler extends Vehicle{
- @Override
- public void start() {
- System.out.println("Two wheeler start logic");
- }
- @Override
- public void stop() {
- System.out.println("Two Wheeler stop logic");
- }
- }
class TwoWheeler extends Vehicle{ @Override public void start() { System.out.println("Two wheeler start logic"); } @Override public void stop() { System.out.println("Two Wheeler stop logic"); } }
FourWheeler.java
- class FourWheeler extends Vehicle{
- @Override
- public void start() {
- System.out.println("Four Wheeler start logic");
- }
- @Override
- public void stop() {
- System.out.println("Four Wheeler stop logic");
- }
- }
class FourWheeler extends Vehicle{ @Override public void start() { System.out.println("Four Wheeler start logic"); } @Override public void stop() { System.out.println("Four Wheeler stop logic"); } }
VehiclManager.java
- public class VehicleManager{
- public static void main(String[] args) {
- Vehicle twoWheeler = new TwoWheeler();
- Vehicle fourWheeler = new FourWheeler();
- twoWheeler.start();
- twoWheeler.stop();
- fourWheeler.start();
- fourWheeler.stop();
- }
- }
public class VehicleManager{ public static void main(String[] args) { Vehicle twoWheeler = new TwoWheeler(); Vehicle fourWheeler = new FourWheeler(); twoWheeler.start(); twoWheeler.stop(); fourWheeler.start(); fourWheeler.stop(); } }
Why do we need method overriding?
Whenever we need to provide different implementation in the subclass for a method which is already defined in the parent class, we need to go for method overriding.
As shown in the above example, start and stop methods definition is different
for Two Wheelers and Four Wheelers than what is provided in the Parent class Vehicle.
So we need to override those methods with new definition.
Runtime polymorphism is the process of resolving a call to an overridden method at runtime rather than at compile-time.
Since overridden
method will have same name and same signature, Its not possible to detect the exact method call during compile time.
At run time, it checks the reference variable pointing to which object and accordingly the exact method call will be resolved.
Since this method resolution happens at run time, its called Runtime polymorphism
.
In the above Example, If we look at below line
- Vehicle twoWheeler = new TwoWheeler();
Vehicle twoWheeler = new TwoWheeler();
We have created the reference
variable of type Vehicle and its named as “twoWheeler”.
This reference
variable is pointing to an object of TwoWheeler class.
Hence below lines will make a call to the start() and stop() methods available inside “TwoWheeler” class.
- twoWheeler.start();
- twoWheeler.stop();
twoWheeler.start(); twoWheeler.stop();
Similary, If we look at below line
- Vehicle fourWheeler = new FourWheeler();
Vehicle fourWheeler = new FourWheeler();
We have created the reference
variable of type Vehicle and its named as “fourWheeler”.
This reference
variable is pointing to an object of FourWheeler class.
Hence below lines will make a call to the start() and stop() methods available inside “FourWheeler” class.
- fourWheeler.start();
- fourWheeler.stop();
fourWheeler.start(); fourWheeler.stop();
We can observe that variable of same “Vehicle” type is calling methods of TwoWheeler class and FourWheeler class based on what object is assigned to it.
This process is called polymorphism as variable of same type is calling methods from different classes at different time.
Since these methods call can be determined only at the runtime
based on what object is pointing to by the reference
variable, its called Runtime polymorphism.
sir,if super claass referenc variable pointing to the sub-class object then sub-class overriden mathode is call its ok.But why it is called superclass not override methode .and why not call sub-class not overriden methode.
plz help me at fast
If method is not overridden ,it means its inherited to subclass and hence it calls subclass method only.
But its not overridden so it executes as it is in super class.