Hi, I am unable to understand the term Object typecasting. Whatever I understood, I am depicting here...Please take a look. I am not sure whether Am I correct or not?
Let see we have Car as superclass...
public class Car{
}
public class Ford extends Car{
public
String fordMethod(){
return "Ford Method.";
}
}
public class Honda extends Car{
public String hondaMethod(){
return "Honda Method.";
}
}
Now, I have client program as:
public class Client {
public static void main(String[] args) {
Car car = new Car();
Ford ford = (Ford)car;
System.out.println("Ford:"+ford.fordMethod());
}
}
Here I am getting exception as:
Exception in
thread "main" java.lang.ClassCastException: com.src.Car incompatible with com.src.Ford
at com.src.Client.main(Client.java:6)
As per my understanding we should not get this exception.
Please explain me object typecasting with this example.
Rahul