Polymorphism in Java
Polymorphism in Java
com
http://www.javatpoint.com/runtime-polymorphism-in-java
Polymorphism in Java
next prev
Polymorphism in java is a concept by which we can perform a single action by different ways . Polymorphism
is derived from 2 greek words: poly and morphs. The word "poly" means many and "morphs" means forms. So
polymorphism means many forms.
There are two types of polymorphism in java: compile time polymorphism and runtime polymorphism. We can
perform polymorphism in java by method overloading and method overriding.
If you overload static method in java, it is the example of compile time polymorphism. Here, we will focus on
runtime polymorphism in java.
Upcasting
When reference variable of Parent class refers to the object of Child class, it is known as upcasting. For
example:
1. class A{}
2. class B extends A{}
1. A a=new B();
void run(){System.out.println("running");}
1/4
3. }
4. class Splender extends Bike{
5.
6.
7.
8.
b.run();
9.
10. }
Test it Now
Output:running safely with 60km.
2/4
8.
System.out.println(obj.speedlimit);
9. }
Test it Now
Output:90
3/4
4/4