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 = 4;
        B b = new B();
        b.i = 10;
        b.j = 20;
        a = b;

        System.out.println("i = " + a.i);
    }
}
class A
{
    int i;
}
class B extends A
{
    int j;
}


i = 4
Compilation Error - since object b can not be assigned to object a
i = a.i
i = 10

Doubts

Problems

Topic: Java Class Inheritance Is-A Relationship In Java Assigning Sub Class Object To Super Class Reference In Java

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