Menu
Question Index
...

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;    }
}


100 and 200
200 and 300
300 and 200
Compilation Error or Runtime Error

Doubts

Problems

Topic: Multilevel Inheritance In Java With Example Program

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App