Menu
Topics Index
...
`

Test Your Skills2553


If you need explanation Read this topic

If you need Answer Take test on this topic

Test Your Skills2553
What will be the output of the following program?
public class TestYourSkills {
    public static void main(String args[]) {
        X obj1 = new Z();
        Y obj2 = (Y) obj1;
        System.out.print(obj1.method1() + " and ");
        System.out.print(obj2.a);
    }
}
class X {
    int a = 100;
    int method1() {    return a;    }
}
class Y {
    int a = 200;
    int method1() {    return a;    }
}
class Z extends Y {
    int method1() {    return a;    }
}

A. 100 and 100
B. 200 and 200
C. 100 and 200
D. 200 and 100
E. Compilation Error or Runtime Error
Topic: Multilevel Inheritance In Java With Example Program

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Ans is E compilation error since the objects are inconvertable type in line 5

Posted by Bharath Yelchuri    2014-04-04 09:41:13


Option is E
since obj1 is reference of class X and "X class reference" cannot refer(point) to "Z class object"
two cases where object of class Z can be referred
1) Z zReference = new Z();
2)ParentOfZ POZReferecne = new Z();

as X class fails to satisfy the above 2 cases answer is E

Posted by Kittu Manoj    2014-04-04 13:03:23


Congratulations Kittu Manoj. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-04-07 13:33:06


Thank You Merit Campus

Posted by Kittu Manoj    2014-04-07 13:33:35


© meritcampus 2019

All Rights Reserved.

Open In App