Polymorphism in Java

(Last Updated On: March 6, 2019)

Learn polymorphism in java with examples. You should have clear concept of polymorphism, types of polymorphism, method overloading, constructor overloading and method overriding. Compile time and run time polymorphism.

 Polymorphism in Java

Polymorphism is one of the oops principles of java. It is a concept where one name can have many forms. It can be a method or a constructor.

In simple word,

having multiple methods or constructors with same name in a class or in parent child relationship in inheritance, both parent and child class have a method with same name. These are the scenarios of polymorphism in java oops. It’s as simple as that.

Let’s understand in more simple way by example.

See the below statements with example, that shows the meaning of having name (methods and constructors) many forms.

1)A class in java can have multiple methods with same name having different parameters or data types. Hence, a method has many forms. This is known as method overloading.

Example:

Class Paint have 3 methods with same name, but, with different arguments.

 

2)A class can has multiple constructors with different parameters in java program. All constructors share the same name as class. Hence, many forms of constructors. This is known as constructor overloading.

Example:

Employee. class has 3 constructors with different number of arguments or different data types if number of arguments are same. Notice that constructor name and class name is same. you can read constructors in java tutorial if you want.

 

3)A base class and a child class have methods with same name. Therefore, method have many forms. This is known as method overriding.

Example:

Both, class Father and Son is having method car() with same name.

 

Types of Polymorphism in Java

There are two types polymorphism in java object-oriented programming i.e. overloading and overriding.

Method overloading, and Constructor overloading come under overloading polymorphism. Overloading is also known as compile time polymorphism.

Method overriding comes under overriding polymorphism and known as run time polymorphism.

 

So for, you understand what is Polymorphism in java oop, Now, read , method overloading , constructor overloading and method overriding in detail with examples. links below.

 

Compile Time and Run Time Polymorphism

Compile Time Polymorphism:

Now you understand that constructor overloading and method overloading comes under compile time polymorphism.  We say compile time polymorphism, because overloaded methods get resolved at compile time itself.

What does it mean, by get resolved?

It means, compiler understand which method to call, out of many overloaded methods during code compilation.

Run time Polymorphism:

Method overriding comes under run time polymorphism. In this method get resolve at run time. means, compiler will not be able to decide from which object’s methods will be called.

Read this interview Question what is compile time and run time polymorphism in java in details with code example.