Read full article about polymorphism and runtime polymorphism in java
Polymorphism is one of the four essential principles of object-oriented programming (OOP) that Java supports. It enables the treatment of objects of distinct classes as objects of a common interface or superclass. Polymorphism in Java is classified into two types: compile-time polymorphism (also known as method overloading) and runtime polymorphism (also known as method overriding).
Compile-time Polymorphism (Method Overloading):
Runtime Polymorphism (Method Overriding):
In this example, even though the reference variable animal is of type Animal, it refers to an instance of Dog. During runtime, the sound() method of the Dog class is invoked.
Polymorphism is an important notion in object-oriented programming because it provides for code flexibility and expansion. By programming to interfaces or basic classes rather than specific implementations, you can develop more generic and reusable code.














