elibraryportal Logo

Polymorphism in Java

Polymorphism is most important features of OOPs concept. Polymorphism is derived from two Greek words poly and morphs.where the word "poly" means many and "morphs" means forms. which Polymorphism can take a single action in different ways. that is called Polymorphism.

Types of Polymorphism

  1. Compile Time Polymorphism
  2. Runtime Polymorphism

1) Compile Time Polymorphism

Compile time polymorphism is also known as static polymorphism is a process in which a call to an overloading method is resolved at compile time rather than at run time.

Example:

Output

Compile time polymorphism

2) Run Time Polymorphism

Runtime polymorphism is also known as Dynamic Method Dispatch which call to an overridden method is resolved at runtime .

Upcasting in java

Runtime polymorphism is provide a upcasting If the reference variable of Parent class refers to the object of Child class, it is known as upcasting

In other words, subclass to a superclass is called upcasting.

Upcasting in java

Syntax:

Example(upcasting):

Output:-:

Upcasting in java

Runtime Polymorphism Example in Java

Example:

Output:-:

Runtime Polymorphism Example in java
Next Topic