Polymorphism explained with real world example in Java

Опубликовано: 22 October 2024
на канале: InvolveInInnovation
16,714
205

Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOPS occurs when a parent class reference is used to refer to a child class object. Polymorphism is a Greek word. poly means “many” and morphs means “forms”.
There are two types of polymorphism. They are
1. Compile-time polymorphism
2. Run-time polymorphism

Method overloading is the called as the compile time polymorphism. Method Overloading is a feature that allows a class to have more than one method having the same name, if their argument lists are different. It is similar to constructor overloading in Java, that allows a class to have more than one constructor having different argument lists.

In runtime polymorphism, an overridden method is called through the reference variable of a superclass. The determination of the method to be called is based on the object being referred to by the reference variable.