Knowee
Questions
Features
Study Tools

What is the output of this program? class A { public int i; private int j; } class B extends A { void display() { super.j = super.i + 1; System.out.println(super.i + " " + super.j); } } class inheritance { public static void main(String args[]) { B obj = new B(); obj.i=1; obj.j=2; obj.display(); } }2 23 3Runtime ErrorCompilation Erro

Question

What is the output of this program? class A { public int i; private int j; } class B extends A { void display() { super.j = super.i + 1; System.out.println(super.i + " " + super.j); } } class inheritance { public static void main(String args[]) { B obj = new B(); obj.i=1; obj.j=2; obj.display(); } }2 23 3Runtime ErrorCompilation Erro

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

The output of this program will be a Compilation Error.

The reason for this is that class B is trying to access a private member 'j' of class A. In Java, a private member cannot be accessed directly outside of its own class, not even by a subclass of its class. Therefore, the line "super.j = super.i + 1;" in class B's display() method will cause a compilation error.

Similar Questions

What will be the output of the following Java program?class A { int i; void display() { System.out.println(i); }}class B extends A { int j; void display() { System.out.println(j); }}class inheritance_demo { public static void main(String args[]) { B obj = new B(); obj.i = 1; obj.j = 2; obj.display(); }}Options02Compilation Error1

Assuming that all the necessary imports are done, what will be the output of the following Java program? class parent { int a; } class child extends parent { int b; void display() { super.a = b + 1; System.out.println(b + " " + i); } } class inheritance { public static void main(String args[]) { child obj = new child(); obj.a=1; obj.b=2; obj.display(); }}Options3 32 22 3Compilation Error

What will be the output of the following code?class A {    void method() {        System.out.println("Class A");    }}class B extends A {    void method() {        super.method();        System.out.println("Class B");    }}public class Main {    public static void main(String[] args) {        B obj = new B();        obj.method();    }}

What will be the output of the program?class Super{ public int i = 0; public Super(String text) /* Line 4 */ { i = 1; } } class Sub extends Super{ public Sub(String text) { i = 2; } public static void main(String args[]) { Sub sub = new Sub("Hello"); System.out.println(sub.i); } }012Compilation fails.

Select the correct answerWhat will be the output of the following Java program? final class base     {         int a;    }        class derived extends base    {        int b;        System.out.println(b + " " + a);      }        class inheritance     {        public static void main(String args[])        {            derived obj = new derived();            obj.display();             }   }OptionsRuntime Error3 32 2Compilation Error

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.