what is class cast exception in java code example

Example: What’s class cast exception?

lass cast excep: you are attempting to cast one class to another. 
what is the precondition to cast a class? IS A relation. 
So when we have a class extending another class. 
as long as there is IS A relation between classes you can cast. 
if reference type is parent, we can downcast it to the child reference type 
RemoteWebDriver is parent of ChromeDriver and FireFoxDriver:  // Polymorphism
RemoteWebDriver driver1 = new ChromeDriver(); // upcasting implicitly done
 FirefoxDriver driver2 = (FireFoxDriver) driver1; // downcasting 
Class cast exceptions means there is no IS A relation // downcasting
you need to have the actual object in order to have the instance.

Tags:

Misc Example