Menu
Question Index
...

What will be the output of the following program?

class TypeCasting
{
    public static void main(String s[])
    {
        A a = new A();
        a.i = 21;
        B b = new B();
        b.i = 43;
        b.j = 25;

        B b2 = (B) a; // LINE X

        System.out.println("i = " + b2.i); // LINE Y
    }

}

class A
{
    int i;
}

class B extends A
{
    int j;
}


i = 21
Compilation Error at LINE Y
i = 43
Runtime Error at LINE X

Doubts

Problems

Topic: Assigning Super Class Reference To A Sub Class Reference In Java

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App