Q2:(4 Points) if we have the following two classes defined class Matrix ( int m-
ID: 3874675 • Letter: Q
Question
Q2:(4 Points) if we have the following two classes defined class Matrix ( int m-0 public Matrix) (m-s public void jump0 System.out.printin(Jumpm) public void punch m--i System.out.printin("punch"+m) public class Agent extends Matrix int a--2 public Agent() { a=0; } public void gesture) ( a++i System.out.println("glare"+ a) public void punch () gesture() a--i system.out.println("pow!"+ a)i Then what is the output, if we execute the following main method public static void main(Stringll args) Matrix one new Agent ); one.punch) Agent two new Agent i two.punch );Explanation / Answer
In Java, constructor is called after initialization of instance variables. So, 'a' is set to '0' on creation of an Agent instance.
Java supports polymorphism. So, 'one.punch()' results in a call to 'punch' method in the Agent class. As such, the output of both 'one.punch()' and 'two.punch()' is exactly same.
Output on execution of given main method is as follows:
glare1
pow!0
glare1
pow!0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.