I went to an interview, where they asked me Questions on Type casting. Its not a tough topic to understand but If you will not get Inside you may get confused when under scrutiny.
So Lets try to understand how to works.
So Lets try to understand how to works.
Type casting in Java is nothing but to cast one type, a class or interface, into another type( class or interface).
It is mainly of two types:
UPCASTING AKA Automatic type conversions:- When you are casting up the reference variable to the inheritance tree or its more general Type means Super-class reference variable is having reference to subclass Object. Its is done Automatically.
To make it more clear here is the simplest example of all for you.
class Animal { }//Super Class
class Dog extends Animal { }// Subclass Extending Super class
class DogTest {//Test Class
public static void main(String [] args) {
Dog d = new Dog();
// All Dogs are Animal hence upcasting is fine.class Dog extends Animal { }// Subclass Extending Super class
class DogTest {//Test Class
public static void main(String [] args) {
Dog d = new Dog();
Animal a1 = d; // upcast ok with no explicit cast Superclass Reference to subclass object
Animal a2 = (Animal) d; // upcast ok with an explicit cast.
}
}
Downcasting AKA Explicit type conversions:- When the destination is larger than the Source size than we have to go for explicit Type casting means when subclass reference variable is having reference for superclasss object than we have to do explicit typecast.
Example;-
class Animal { }//superclass
class Dog extends Animal { }//subclass
class DogTest {//test class
public static void main(String [] args) {
Animal a = new Animal();//superclass object
Dog d = new Dog();//subclass object
// Every animal can't be Dog so be careful with downcasting
// other wise it may throw ClassCastException at runtime.
Dog d1 = a; // won't compile, as downcast needs explicit casting
class Dog extends Animal { }//subclass
class DogTest {//test class
public static void main(String [] args) {
Animal a = new Animal();//superclass object
Dog d = new Dog();//subclass object
// Every animal can't be Dog so be careful with downcasting
// other wise it may throw ClassCastException at runtime.
Dog d1 = a; // won't compile, as downcast needs explicit casting
Animal a1 = d; // upcast ok with no explicit cast
Dog d2 = (Dog) a1; // downcast needs explicit casting, works fine.
Dog d2 = (Dog) a1; // downcast needs explicit casting, works fine.
}
}
Q:- what is java.lang.ClassCastException?
Well this is Runtime Exception that is thrown when you try to explicitly typecast into another class if they are not in the inheritance tree.
In above example this may compile.
Dog d2 = (Dog) a1;
but may fail at runtime throwing ClassCastException if at runtime its not the best code.
// Animal can't EVER be a String
String s = (String) animal; // this line won't compile.
So I hope it helps you clear your type casting doubts. When ever in doubt in interview stick to the basics. Every Thing big always starts from basics.