Menu
Question Index
...

What will be the output of the following program?

class ClassInheritance
{
    public static void main(String s[])
    {
        A a = new A();
        a.i = 21;
        B b = new B();
        b.j = 25;
        printI(a);
        printI(b);  // LINE X
        printJ(b);
    }

    public static void printI(A a1)
    {
        System.out.println(a1.i);
    }

    public static void printJ(B b1)
    {
        System.out.println(b1.j);
    }
}

class A
{
    int i;
}

class B extends A
{
    int j;
}


21
0
25
21
21
25
Compilation Error at LINE X - since printI can be only called for objects of type A.
ClassCastException will be thrown at runtime

Doubts

Problems

Topic: Passing Sub Class Object As Super Class Reference Is-A Relationship In Java

Read this topic Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App