Get 50% Discount on Our Udemy Course to Memorize the Periodic Table in 30 minutes Click Here. COUPON CODE: ALCHEMY

How to remember the difference between Runtime polymorphism and Compile time polymorphism in Java?

0 votes
400 views
asked Dec 3, 2015 by nanospeck (4,880 points)

Often we might get confused by the following

Compiletime polymorphism class overloading in java
Runtime polymorphism class overriding in java

Both class overriding/overloading might get mixed up and can cause confusion.
What is an easy hack to remember this?

1 Answer

0 votes
answered Dec 3, 2015 by nanospeck (4,880 points)

The hack is to create a trick to remember only 'Runtime Polymorphism'. 
Here is simple piece of code about 'Bikes' relating to runtime polymorphism

class Bike {
public ride(){}
}

class HarleyDavidson extends Bike {
// Look!!! class-overriding below
public ride(){
System.out.println("Enable 6th gear");
}

}

Now here comes the trick.Runtime polymorphism can be imagined as something that always moves. Eg. our bike is a perfect example. You can ride it around. It's dynamic and it moves. 
So here 'ride' is the keyword.

Relate class-overriding with the method 'ride

The 'ride' method in the above example is an instance of class-overriding hence its very easy to remember now that Runtime Polymorpism is related with class-overriding

Related questions

0 votes
1 answer 176 views
0 votes
1 answer 595 views
0 votes
1 answer 3,284 views
...