test
1
Bora Sabrioglu wrote:Does anyone know why this happens?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:If you're asking "why did the architects do it that way?"...
The class being created is a B, therefore any call to init() will run B.init(), which exhibits exactly the behaviour you just described.
Bora Sabrioglu wrote:then now, because the object is of type B , the B version of init() will be invoked. (That's because of polymorphism.)
Paul Clapham wrote:
Bora Sabrioglu wrote:then now, because the object is of type B , the B version of init() will be invoked. (That's because of polymorphism.)
This is all you need to know.
Stephan van Hulst wrote:Note that you should probably never call overridable methods from a constructor, or pass this as an argument to methods.
Jesper de Jong wrote: For example: can you explain the output of the following program?
Not when I tried it, it didn't.Bora Sabrioglu wrote: . . . It prints null . . .
Campbell Ritchie wrote:Not when I tried it, it didn't.
sreenivasarao mydukuri wrote:Super class (Class A) constructor was calling sub class (Class B ) int() method.
Campbell Ritchie wrote:
Not when I tried it, it didn't.Bora Sabrioglu wrote: . . . It prints null . . .
Campbell Ritchie wrote:Nothing. I got a NullPointerException instead.
Stephan van Hulst wrote:If you need to call a method of the class being constructed, make that method private or final.
Campbell Ritchie wrote:Add the @Override annotation to every method you think is an overriding method, and see what happens.
error: method does not override or implement a method from a supertype
Right.Campbell Ritchie wrote:Because init in A is private, init in B cannot override it.
Okay... so it seems that the assumption that I made above was correct?Campbell Ritchie wrote: The A constructor can only therefore call the A version.
Am I right in assuming that the new insight that I had due to the posts above, that always the type of the object determines which version of a method is invoked (if object of type B, the B's init() is invoked) is only valid in the case of overriding? Or is this a wrong assumption?