Polymorphism

The word polymorphism means having many forms.
It is one of most the important features of Object-Oriented Programming.
The word “poly” means many and “morphs” means forms, So it means many forms.
As a real-time example, a person at the same time can have a different role. At the same time he can be a father, a son, a husband or an employee. So the same person posses different behavior in different situations. This is called polymorphism.
In same thing, polymorphism allows a programmer to define one method having multiple implementations.

There are basically 2 types of polymorphism.
1. Method Overloading
2. Method Overriding

1. Method Overloading:
When a class has multiple methods with same name, but different parameters, then it is called as method overloading.

Example:

class Main {
    public static void main(String[] args) {
        MyObject object=new MyObject();
        double area1=object.findArea(10,5); //passing 2 parameters
        double area2=object.findArea(10,5,3); //passing 3 parameters
        System.out.println("Area of object 1 is "+area1);
        System.out.println("Area of object 2 is "+area2);

    }

}
class MyObject{
    public double findArea(double length, double width){
        return length*width;
    }
    public double findArea(double length, double width, double height){
        return length*width*height;
    }
}
Output:
Area of object 1 is 50.0
Area of object 2 is 150.0

In this example, we see that, only passing the parameters can identify the different methods and method is called.

2. Method Overriding:
When same method name is used in different class name and that classes are derived from parent class, then only the method is executed which class is initiated from parent class.

Example:

class Main {
    public static void main(String[] args) {
       Animal animal;
       
       animal=new Animal();
       animal.eat();

       animal=new Tiger();
       animal.eat();

       animal=new Cow();
       animal.eat();

    }

}
class Animal{
    public void eat(){
        System.out.println("Animal can eat");
    }
}
class Tiger extends Animal{

}
class Cow extends Animal{

}

Output:

Animal can eat
Animal can eat
Animal can eat

In the above code, Tiger and Cow class does not have their own methods. So when we initiate animal object and call the “eat” method, then each time parent class method is being called.

Now see this code snippet.

class Main {
    public static void main(String[] args) {
       Animal animal;

       animal=new Animal();
       animal.eat();

       animal=new Tiger();
       animal.eat();

       animal=new Cow();
       animal.eat();

    }

}
class Animal{
    public void eat(){
        System.out.println("Animal can eat");
    }
}
class Tiger extends Animal{
    public void eat(){
        System.out.println("Tiger eats deer");
    }

}
class Cow extends Animal{
    public void eat(){
        System.out.println("Cow eats grass");
    }

}
Output:
Animal can eat
Tiger eats deer
Cow eats grass

I have added same method name for each sub class. And calling them from main method. Then parent method is overridden and child method gets executed.

about author

admin

salmansrabon@gmail.com

If you like my post or If you have any queries, do not hesitate to leave a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *