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.i = 32; // LINE X
        b.j = 25;
        printI(a);
        printI(b); // LINE Y
        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;
}


Compilation Error at LINE X - since i is not accessible from b
21
21
25
21
32
25
Compilation Error at LINE Y - since printI can be only called for objects of type A.

Doubts

Problems

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

Read this topic 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