What is Java PolymorphismPolymorphism is a feature of Object Oriented Programming where an Object has same interface for different underlying forms. In this tutorial, we will discuss about the concept and types of polymorphism in java.
So let’s take an real world example to understand this concept better.
Let’s take an example of a website. We go through different websites in our day to day life like facebook, gmail, you tube etc. Each website has different buttons like add comment, like, login, logout etc. But it is simply a “Button” and you have to just press the button. So, button is an interface for different underlying forms. Button can be used to add a comment, can be used to like picture etc.
So, a Button on a website can be a simple example of Polymorphism.
Types of polymorphism in java
There are two types of polymorphism in java as discussed below.
- Compile time Polymorphism or static binding
- Run time Polymorphism or dynamic binding
Let’s discuss them one by one.
Compile time Polymorphism or Static Binding
When an object knows which method will be called at the time of compile, it is known as compile time polymorphism or static binding.
It means you bind a method with an object and at run time same method will be invoked by that object.
Compile time polymorphism is achieved by method overloading in java. We will discuss about static binding, compile time polymorphism and method overloading in details later.
Run time Polymorphism or dynamic binding
When object does not know which method will be invoked at run time, it is called run time polymorphism or dynamic binding. Binding does not happen at compile time because an object is not aware of it. At run time, it will be decided which method will be invoked by an object.
Dynamic binding is achieved by method overriding. We will discuss about dynamic binding, compile time polymorphism and method overriding in details later.