can we down cast super class if sub class belonngs to same hierarchy ?
example :
class Building { }
public class Barn extends Building {
public static void main(String[] args) {
Building build1 = new Building();
Barn barn1 = new Barn();
//Barn barn2 = (Barn) build1; // line number 10
Object obj1 = (Object) build1;
//String str1 = (String) build1; // line number 12
Building build2 = (Building) barn1;
}
}
Answer here states that only line 12 commented will make the code compile. But code is getting compiled only if even the line 10 is commented. please help.