Menu
Question Index
...

What will be the output of the following program?

class ATest
{
    public static void main(String[] args)
    {
        A a = new A();
        a.print();
        B b = new B();
        b.print();
        A t = b;
        t.print();
        a = b;
        a.print();
        b = (B) t;
        b.print();
        b = (B) a;
        b.print();
    }
}

class A { void print() { System.out.print("A"); } }
class B extends A { void print() { System.out.print("B"); } }


ABABBB
ABBABB
ABBBBB
Compilation Error
Runtime Error - ClassCastException

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