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; // LINE X
        b.j = 20;

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

class A
{
    int i;
}

class B // LINE Y
{
    int j;
}


Compilation Error at LINE Y - since class B is not extending class A
i = 4
i = 10
Compilation Error at LINE X - since the member variable i is not accessible in b's object

Doubts

Problems

Topic: Java Class Inheritance

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App