What is polymorphism?

Poly means many and morphism means form.

Polymorphism is is Greek word which means “One object having different behavior”.

for ex : A human is a best example of polymorphism like a human can be a student, teacher, customer, or a friend, like a person can perform many task. In programing language one object can also perform many task.

THERE ARE TWO TYPE OF POLYMORPHISM IN JAVA

  1. Compile time polymorphism

    compile time polymorphism is also known as static polymorphism or early binding polymorphism.

    In compile time polymorphism compiler has strong idea about the method call based on the parameter of the method.

    We can achieve compile time polymorphism by using METHOD OVERLOADING.

Method overloading —> Whenever we write more than one method in same class as well as in the another class in such a way that method name must be same and parameter must be different it is called as method overloading.

  1. Run time polymorphism

    Run time polymorphism is also known as dynamic polymorphism or late binding polymorphism.

    In Run time polymorphism compiler does not have any idea about object at run time JVM decides which method is invoked.

    We can achieve Run time polymorphism by using method overriding.

Method overriding —> whenever the super class method and subclass method having same name and same return type then it is called as method overriding. We can’t perform method overriding without inheritance. In method overriding signature must be same like method name and return type must be compatible. We can’t override the method in same class. We can only override a static method.