As Java is an Object oriented programming language and supports both Inheritance and Polymorphism, It?s easy that Super class reference variable is pointing to Sub Class object but point here is that there is no way for Java compiler to know that a Super class variable is pointing to Sub Class object. Which means you cannot call method which is declared on sub class. In order to do that, you first need to cast the Object back into its original type. This is called type-casting in Java. Type casting comes with the risk of ClassCastException in Java, which is quite common with method which accept Object type and later type cast into more specific type. Another worth noting point here is that from Java 5 onwards you can use Generics to write type-safe code to reduce amount of type casting in Java which also reduces risk of java.lang.ClassCastException at runtime.
***************************** Why TypeCasting *****************************
to get access of fields and methods declared on target type or class. You can not access them with any other type. Let´s see a simple example of type casting in Java with two classes Base and Derived which shares same type hierarchy.
Type casting example in Java:
------------------------------------------
Base class
|
|
v
Derived class
Now look at following code :
Base b = new Derived(); //reference variable of Base class points object of Derived class
Derived d = b; //compile time error, requires casting
Derived d = (Derived) b; // type casting Base to Derived
Above code type casting object of Derived class into Base class and it will throw ClassCastExcepiton if b is not an object of Derived class. If Base and Derived class are not related to each other and doesn´t part of same type hierarchy, cast will throw compile time error. for example you can not cast String and StringBuffer, as they are not from same type hierarchy.
******************** Type-casting and ClassCastExcepiton in Java ********************
As explained in last section type casting can result in ClassCastException in Java. If the object you are casting is of different type. ClassCastException is quite common while using Java collection framework classes e.g. ArrayList, LinkedList or HashSet etc because they accept object of type java.lang.Object, which allows insertion of any object into collection. let´s a real life example of ClassCastException in Java during type casting :
ArrayList names = new ArrayList();
names.add("XYZ"); //adding String
names.add(1); //adding Integer
String name = (String) names.get(0); //No issue
name = (String) names.get(1); // throw ClassCastException because you can not convert Integer to String
In above example we have an ArrayList of String which stores names. But we also added an incorrect name which is Integer and when we retrieve Object from collection they are of type java.lang.Object which needs to be cast on respective for performing operation. This leads into java.lang.ClassCastException when we try to type cast second object, which is an Integer, into String. This problem can be avoided by using Generics in Java.
5