Which of the following statements sec cause compilation err
Which of the following statements (sec **) cause compilation errors class X{} class Y extends X{} class Z extends X{} class Test{public static void main(String args[]) {X x = new X(); Y y = new Y(); Z z = new Z(); Object obj 1 = new Y(); Object obj 2 = new X();//** The below statements creating//references o1, o2, ..., o12 X o1 = new Y(); X o2 = z; Y o3 = new Z(); Y o4 = (Y)x; Y o5 = (Y) obj 1; Z 06 = z; Z o7 = (Z) y; Z 08 = (X) y; Object o9 = y; Object o10 = (X) obj 1; X o11 = (X)obj 2; Z o12 = (Z) obj 1;//}
Solution
Program:IN the Following Program the highlighted statements cause the compilation errors
class X{}
class Y extends X{}
class Z extends X{}
class Test{
public static void main(String args[])
{
X x=new X();
Y y=new Y();
Z z=new Z();
Object obj1=new Y();
Object obj2=new X();
//**The below statements creating
// references o1,o2,o3,.......,o12
X o1=new Y();
X o2=z;
Y o3=new Z(); // incompatible types: Z cannot be converted to Y
Y o4=(Y) x;
Y o5=(Y) obj1;
Z o6=z;
Z o7=(Z) y; // incompatible types: Y cannot be converted to Z
Z o8=(X) y; // incompatible types: X cannot be converted to Z
Object o9=y;
Object o10=(X) obj1;
X ol1=(X) obj2;
Z o12=(Z) obj1;
}
}
