Note that, whenever you construct an object of derived class,
it must complete constructor call(or calls) of the base class(or classes)
from top to bottom.
Here is what is happenning -
(1)In main you call default non parameterized constructor of Suresh.
(2) Suresh extends Bindu, hence Bindu's corresponding non-parameterized constructor is called.
(2) In Bindu(), you call constructor Bindu(int x) with value 1 (via this(1), constructors are not overridden), which in turn calls method "method()",
but method() is a class method/function and is overridden in Suresh, hence call is resolved to Suresh.method() at runtime and first line printed is - " Suresh Mehod". The next statement in method() of suresh is
supposed to print string s -- "Babu", which is an instance variable, it does not exist at this moment because the object of Suresh is not yet instantiated(can not be created w/o completing its Base class constructors). Hence prints null.
(3) Next, after resolving polymorphic "method()" call,the control returns to next executable statement of Base(int x), from where "method()" was called. This prints "In Bindu(x)" on next line.
(4) Again, control returns to Base(), from where Base(1) was called, again this.method() call gets resolved to its polymorphic derived class implementation and prints " Suresh Method". Agin same explanation (as above) applies for "System.out.println(s);" call in Suresh.method(), hence prints null.
(5) Next executable statement in Base() (we haven't finished it yet!!!), is -
"System.out.println(this.getClass()); ", which *does* refer to the actual class
which is loaded by JVM - hence prints "class Suresh.
(6)Next is straight forward, prints - "In Bindu", next statement prints Bindu's own instance variable s, which is DevJani. Member variables *do not* display any virtual, polymorphic behaviour.
Yes! Done with base class. So far so good.
(7) Now, constructor of Suresh, first executable statement is "System.out.println("In Suresh");", prints - "In Suresh, next statement is - "System.out.println(s);", which refers to instance variable s, having value "Babu". The value is printed. Note that, this time value is printed because call to the constructor of base class has been completed and hence instance variables do exist.
I hope this makes it clear. Answers to your specific questions -
Please clarify the follwoing doubts...
In Bindu() constructor..
this.method() .. this referes to Current OBject Here it is Suresh.. It is calling
Suresh's method
1. But when you use this(1).. according to above point it should call Suresh(1),
but it is calling Bindu(1).. why?
Constructors are diffrent from methods and are neither inherited, nor do they display any virtual, polymorphic behaviour. Hence, here a call in base class Bindu() gets resolved to its own parametrized constructor.
2. Why Out put is Null in Subclass method..
Because, at that time, the call to the base class constructor is not complete, hence derived class object, and its instance variables do not exist.
HTH,
- Manish
[This message has been edited by Manish Hatwalne (edited October 06, 2001).]