class Base
{
int i = 99;
public void amethod()
{
System.out.println("Base.amethod()");
}
Base()//line A
{
amethod();
}
}//end of class base
public class Derived extends Base
{
int i = -1;
public static void main(
String argv[])
{
Base b = new Derived();//line 1
System.out.println(b.i);//line 2
b.amethod();//line 3
}
public void amethod()
{
System.out.println("Derived.amethod()");//line B
}
} //end of Derived class
OPTION A:
Derived.amethod()
-1
Derived.amethod()
OPTION B:
Derived.amethod()
99
OPTION C:
Derived.amethod()
-99
OPTION D:
Derived.amethod()
OPTION E:
Compile time error
The correct answer given is
option B
But How this can Be?
I traced it as follows:
First when line 1 is executed control transfers to line A which calls "amethod".
"amethod" in derived class is called.SO line B iis executed first,thus printing "derived.amethod()".
Then line 2 is executed and o/p should be "-1" right?How it can be "99"?
Then line 3 is executed,and should print "derived.amethod()".
So on the whole the o/p I guessed is
Derived.amethod()
-1
Derived.amethod()
Anybody please explain this.