Type Casting Interview Questions In Java
Type casting helps calling methods of a class using generic reference thereby maintains Polymorphism.
Question 1. We have a requirement to display all the features of a Bird and a class is designed as shown below, how to display all the features of each Bird?
interface Bird{ void walk(); } interface Fly{ void fly(); } interface Swim{ void swim(); } class Duck implements Bird, Fly, Swim{ @Override public void swim() { System.out.println("Duck.swim()"); } @Override public void fly() { System.out.println("Duck.fly()"); } @Override public void walk() { System.out.println("Duck.walk()"); } } class Pigeon implements Bird, Fly{ @Override public void fly() { System.out.println("Pigeon.fly()"); } @Override public void walk() { System.out.println("Pigeon.walk()"); } }For displaying all the features of Pigeon and Duck, we only need to know what all operations Birds can support like Fly, Swim etc?
Based on type checking, we can call particular operation and display all features.
class Zoo{ public static void main(String[] args) { Bird bird1 = new Duck(); bird1.walk(); if(bird1 instanceof Fly){ ((Fly)bird1).fly(); } if(bird1 instanceof Swim){ ((Swim)bird1).swim(); } Bird bird2 = new Pigeon(); bird2.walk(); if(bird2 instanceof Fly){ ((Fly)bird2).fly(); } if(bird2 instanceof Swim){ ((Swim)bird2).swim(); } } }Interface typecast helps to achieve this kind of behaviour.
Question 2.
What is the output of below program? will there be any error/exception? if yes then compile time or run time and why?
class SampleClass1 { public void test(){} } class SampleClass2 { public void test(){} } class MainTest { public void main(String[] args) { SampleClass1 sc1 = new SampleClass1(); SampleClass2 sc2 = (SampleClass2) sc1; } }It will give Compile Time Error: "Cannot cast from SampleClass1 to SampleClass2".
Casting is possible only if there is Parent-child relationship between classes.
Question 3.
What is the output of below program? will there be any error/exception? if yes then compile time or run time and why?
interface SInterface1 {} class SampleClass1 {} class MainTest1 { public static void main(String[] args) { SampleClass1 sc1 = new SampleClass1(); SInterface1 sc2 = (SInterface1) sc1; } }It will NOT give Compile Time Error but will give Runtime Exception:
"java.lang.ClassCastException: SampleClass cannot be cast to SInterface1".
Question here is why it didn't gave Compile time error?
Compiler is really not sure here to give compile error because sc1 at runtime can be reference of
class SampleClass2, say (class SampleClass2 extends SampleClass1 implements SInterface1) in that case typecasting is perfectly valid. So Compiler doesn't give Compile error in this case, but when you run the program it sees that sc1 doesn't point to class that implements SInterface1 and that is why it cannot be typecasted.
Valid typecase possible at runtime,
interface SInterface1 {} class SampleClass1 {} class SampleClass2 extends SampleClass1 implements SInterface1{} class MainTest1 { public static void main(String[] args) { SampleClass1 sc1 = new SampleClass1(); sc1 = new SampleClass2(); SInterface1 sc2 = (SInterface1) sc1; } }
Question 4.
What is the output of below program? will there be any error/exception? if yes then compile time or run time and why?
class ClassA{} class ClassB{} interface InterfaceI{} class MainTest11 { public static void main(String[] args) { InterfaceI i = (InterfaceI)(new ClassA()); ClassA b = (ClassB)(new ClassA()); } }It will give Compile Time Error: "Cannot cast from ClassA to ClassB" at line 9.
It will give Runt Time ClassCastException: "ClassA cannot be cast to InterfaceI" at line 8.
Check below image for better understanding of how Compiler treats casting to
Reference and Class,
Question 5.
What is the output of below program? will there be any error/exception? if yes then compile time or run time and why?
interface Interface1 { } interface Interface2 { } class Class1 implements Interface1 { } class Test{ public static void main(){ Class1 c1 = new Class1(); String str = new String("Hello"); //OR Integer str = new Integer(1); Interface2 x = (Interface2)c1; //why compiler does not complain here? Interface2 y = (Interface2)str; //why compiler complains here? } }It will not give Compile time Error at line 10 but gives Compile error at line 11, why?
According to Question 4, which explains rules of typecasting,
Interface2 x = (Interface2) c1;
Compiler will not care what c1 is, it just validates "whether c1 can be object of a Class which is subclass of c1's class type and implements Interface2"?
For line 10, that is possible because there can be class like,
Class A extends Class1 implements Interface2{} c1 = new A(); Interface2 x = (Interface2)c1;
For line 9, that is not possible, str can't be object of class,
- which extends String class and (This is not possible)
- Implements Interface2. (This is possible)
If we declare Class1 final, then compiler will complain at line 10 as well.
You may also like to see
Advanced Java Multithreading Interview Question-Answer
How is ambiguous overloaded method call resolved in java?
Exception Handling Interview Question-Answer
Method Overloading - Method Hiding Interview Question-Answer
Method Overriding rules in Java
Interface interview questions and answers in Java
Enjoy !!!!
If you find any issue in post or face any error while implementing, Please comment.
Post a Comment