Knowee
Questions
Features
Study Tools

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.

Question

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.

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

Solution

The program will not compile. The reason is that in Java, the constructor of the superclass (Super) is called before the constructor of the subclass (Sub). If the superclass does not have a default constructor (a constructor with no arguments), the subclass must explicitly call one of the constructors of the superclass using the "super" keyword.

In this case, the Super class does not have a default constructor, and the Sub class does not call the existing constructor of the Super class. Therefore, the Java compiler will throw an error.

To fix this, you can add a call to the superclass's constructor in the Sub class's constructor, like this:

public Sub(String text)
{
    super(text);
    i = 2;
}

With this modification, the program will compile and the output will be "2".

This problem has been solved

Similar Questions

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

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();    }}

elect the correct answerPredict the output of the given Java Programs. class A {    public void Print() {        System.out.println("B's Print()");     }  }   class A extends B {    public void Print() {        System.out.println("A's Print()");      }  }   class C extends A {    public void Print() {        super.super.Print();        System.out.println("C's Print()");     }  }   public class Main {    public static void main(String[] args) {         C c = new C();         c.Print();      } }OptionsB's Print()C's Print()A's Print()Compiler Error

What is the expected output?class Animal { Animal() { System.out.println("Animal"); }}class Wild extends Animal{ Wild() { System.out.println("Wild"); super(); }}public class Test { public static void main(String args[]) { Wild wild = new Wild(); }}

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

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.