java runtime polymorphism code example

Example 1: what is runtime polymorphism

Runtime polymorphism is a process of which take
cares the overriding method at run time
rather than compile time.
 In method overriding,
a subclass overrides a method with the same 
signature as that of in its superclass. 
During compile time, the check is made 
on the reference type. But, in the runtime,
JVM figures out the object type and would 
run the method that belongs to
that particular object.

Example 2: polymorphism in java

POLYMORPHISM: It is an ability of object to behave in multiple
form. The most common use of polymorphism is Java, when a
parent class reference type of variable is used to refer to a child
class object.
In my framework
  E.g.: WebDriver driver = new ChromeDriver();
We use method overloading and overriding to achieve
Polymorphism.

There are two types of achieving Polymorphism
Upcasting & Downcasting
Upcasting is casting a subtype to a supertype, 
going up in the inheritance tree.
It is done implicitly
 in order to use method available on any
 interface/class, the object should be of
 same class or of class implementing the interface.  
   WebDriver driver = new ChromeDriver();
or
	TakeScreenShot ts = new ChromeDriver();
    
    Downcasting is casting to a subtype, 
going down in the inheritance tree.
It is done to access sub class features.
It has to be done manually

ChromeDriver driver = (ChromeDriver)Webdriver;

Tags:

Misc Example