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;

        a = convertToA(b);  // LINE X
        a = convertToA((B) a);  // LINE Y

        System.out.println("i = " + a.i);
    }

    public static A convertToA(B b1)
    {
        return b1; // LINE W
    }
}

class A
{
    int i;
}

class B extends A
{
    int j;
}


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

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