Given1. class Pro{2. 3. public static void main(String args[]){4. System.out.print(new A(5).y);5. System.out.print(new A().x);6. } 7. } 8. 9. class A{10. A(int i){11. y = i;12. System.out.print("A");13. super();14. }15. int y;16. int x = 10;17. } 18. Which is the output?*A510Compilation fails due to multiple errors.Compilation fails due to an error on line 5.A5 followed by an exception.Compilation fails due to an error on line 13.
Question
Given1. class Pro{2. 3. public static void main(String args[]){4. System.out.print(new A(5).y);5. System.out.print(new A().x);6. } 7. } 8. 9. class A{10. A(int i){11. y = i;12. System.out.print("A");13. super();14. }15. int y;16. int x = 10;17. } 18. Which is the output?*A510Compilation fails due to multiple errors.Compilation fails due to an error on line 5.A5 followed by an exception.Compilation fails due to an error on line 13.
Solution
The compilation fails due to an error on line 13. In Java, the call to the superclass constructor, super(), must be the first statement in a subclass constructor. In the given code, super() is not the first statement in the constructor of class A, hence it will cause a compilation error.
Similar Questions
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 is the output of the following code? 1: class Mammal { 2: public Mammal(int age) { 3: System.out.print("Mammal"); 4: } 5: } 6: public class Platypus extends Mammal { 7: public Platypus() {8: System.out.print("Platypus"); 9: } 10: public static void main(String[] args) { 11: new Mammal(5); 12: } 13: } *1 pointA. PlatypusB. MammalC. PlatypusMammalD. MammalPlatypusE. The code will not compile because of line 8.F. The code will not compile because of line 11
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?class A{ int i = 10; public void printValue(){ System.out.print("Value-A"); }}class B extends A{ int i = 12; public void printValue(){ System.out.print("Value-B"); }}public class Test{ public static void main(String args[]){ A a = new B(); a.printValue(); System.out.print(a.i); }}Value-B 11Value-B 10Value-A 10 Value-A 11
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
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.