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

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

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

class A
{
    int i;
}

class B extends A
{
    int j;
}


i = 21
i = 43
a.i = 21
Compilation Error at LINE Y

Doubts

Problems

Topic: Assigning Sub Class Object To Super 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