Given:
and the code fragment:S2 sobj = new S2();
sobj.display(10, 100);
What is the result?
Given:
and the code fragment:S2 sobj = new S2();
sobj.display(10, 100);
What is the result?
D
The correct answer is A. it will print Child10 Child100 Parent100
A is correct, tested
Answer is A , straight forward
Answer is A. class S1 { protected void display(int x) { System.out.println("Parent" + x); } } public class S2 extends S1 { public void display(int x, int y) { this.display(x); display(y); super.display(y); } public void display(int x) { System.out.println("Child" + x); } public static void main(String[] args) { S2 sobj = new S2(); sobj.display(10, 100); // Child10 Child100 Parent100 } }
The correct answer is letter D, a super has to be instantiated first in a method.
JVM will automatically create a default constructor to initialize all fields (which in these cases there are no any attribute fields) so there will be no any compile time error.
The answer is A, I tested. But why don't we get compilation error as super and this are supposed to be first statement in a constructor ? Can someone explain?
super.display() invoke S1 display method. need to be aware its not be using "super()". The same thing is happening with this.display() its invoke a method too, not be using "this()"
display() is been overloading & overriding. JVM will automatically create a default constructor to initialize all fields (which in these cases there are no any attribute fields)
Answer is A. UAK94 provided us the source code, in their comment.
Answer should be A. The Main method not found is just the omitted part of this question.