Menu
Topics Index
...
`

Test Your Skills


If you need explanation Read this topic

If you need Answer Take test on this topic

Test Your Skills
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 extends X {
    int a = 200;
    int method1() {    return a;    }
}
class Z extends Y {
    int a = 300;
    int method1() {    return a;    }
}

A. 100 and 200
B. 200 and 300
C. 300 and 200
D. 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 C.. we r ovrriddng the method1 and created object for Z and Y classes.. so first 300 vl be printd and then 200 in Y class vl be printd..

Posted by Om Prakash Om    2014-01-27 09:15:08


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

Posted by Merit Campus    2014-01-28 04:03:20


Thank u MC:)

Posted by Om Prakash Om    2014-02-04 12:57:04


© meritcampus 2019

All Rights Reserved.

Open In App