📜  什么是类转换异常? - 无论代码示例

📅  最后修改于: 2022-03-11 15:00:25.203000             🧑  作者: Mango

代码示例1
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.