Monday 11 February 2013

3. What will happen when you attempt to compile and run the following code?

3. What will happen when you attempt to compile and run the following code?



Choices:
a. 300
b. 240
c. 120
d. 180
e. Compilation error
f. None of the above
Answer:-

A is the correct choice.
In the code, MyChild class overrides the addMe(int x, int y) method of the MyParent class.
And in both the MyChild and MyParent class, addMe() method is overloaded.

There is no compilation error anywhere in the above code.

First Statement in Main Method:-(MyChild myChi = new MyChild(10, 20, 30);)
First:-
On execution, first, the object of MyChild class will be constructed.
Second:-
Please note that there is a super() call from the constructor of MyChild class, which will call the constructor of MyParent class.
Third:-
This will cause the value of z variable of MyChild class to be 30 and x, y variables of MyParent class will become 10 and 20 respectively.

Second Statement In Main Method:(MyParent myPar = new MyParent(10, 20);)
First:-
The next statement will again call the constructor of MyParent class with same x and y values.

Third Statement In Main Method:( int x = myChi.addMe(10, 20, 30);)
First:-
This is followed by execution of addMe() method of MyChild class with x as 10, y as 20 and z as 30.
Second:-Also x and y are inherited by MyChild class from the MyParent class.
Third:-Thus in the addMe() method of the MyChild class, the value of this.x will be 10, this.y will be 20 and this.z will be 30.
Fourth:-
The return value of this method will be "10 + 10 + 20 + 20 + 30 + 30", which is equal to 120.
Thus x will become 120.

Fourth Statement In Main Method:( int y = myChi.addMe(myChi);)
First:-
This is followed by the invocation of the other addMe() method which takes object reference of the MyChild class.
Second:-
From this method, the method which was called earlier is invoked.
Third:-
This call is exactly the same as the earlier one.
Fourth:-
Thus the value of y will also be 120 like x.

Fifth Statement In Main Method:( int z = myPar.addMe(myPar);)
First:-
Now the addMe() method of MyParent class is invoked.
Second:-
This method invokes another addMe() method of the same class.
Its equivalent to the invocation of addMe(int x, int y) method with x as 10 and y as 20.
Third:-
Also the value of instance variables x and y of My Parent class is 10 and 20 respectively.
Fourth:-
The value of z will be evaluated to "10 + 10 + 20 + 20", which is equal to 60.

Fifth Statement In Main Method:(System.out.println(x + y + z);)
First:-
Thus the value of x, y and z after all the invocations will be 120, 120 and 60 respectively.

As a result of this finally, "120 + 120 + 60" which is equal to 300 will be printed.
Thus A is the correct choice.


Overloading - Method overloading deals with the notion of having two or more methods(functions) in the same class with the same name but different arguments.


Overriding - While Method overriding means having two methods with the same arguments, but different implementation. One of them would exist in the Parent class (Base Class) while another will be in the derived class(Child Class).@Override annotation is required for this.

No comments:

Post a Comment