test Base constructor
der
Derived constructor
der
Baseprintsec
der
If i am calling printMe() from constructor of base class, it is still calling printMe() of derived. Even though there is no object of derived class till this line, still method of derived class is getting called.
Basically, polymorphism applies -- even before instantiation of the object has completed. Since the derived class overridden the base printme() method, it is always called.
I really doubt if polymorphism is applied before instance creation.
I thought first objects gets created and then there binding is done using polymorphism.
Any more thoughts?
Abhi Deshmukh
Greenhorn
Joined: May 05, 2010
Posts: 7
posted
0
I am agreeing with Henry.
Have a look at below code snippet where we are using "this" explicitly to clear picture.
And we know that within an instance method or a constructor, this is a reference to the current object (Derived object in our case) — the object whose method or constructor is being called. We can refer to any member of the current object from within an instance method or a constructor by using this.
Since printMe() method is overridden in Derived class it get executed.
if you call the derived class constructor . first it goes to the super class constructer if any.
once it get it executes the code thats what you are getting.
and then it runs its own constructor that is the derived constructor.Since the printMe() is a overridden method it will always be executed.
Good luck!!
A small leak can sink a Gigantic ship.>
Greetings all. I've just joined coderanch and this is my first post. This is a good topic to understand polymorphism. But correct me if I am wrong, isn't it a bad idea to call overridable instance methods from a constructor, because of the reason mentioned by Henry?
Basically, polymorphism applies -- even before instantiation of the object has completed. Since the derived class overridden the base printme() method, it is always called.
As the overridden method in derived class can use an uninitialized instance variable.
YogeshS Pandit wrote:Greetings all. I've just joined coderanch and this is my first post. This is a good topic to understand polymorphism. But correct me if I am wrong, isn't it a bad idea to call overridable instance methods from a constructor, because of the reason mentioned by Henry?
Basically, polymorphism applies -- even before instantiation of the object has completed. Since the derived class overridden the base printme() method, it is always called.
As the overridden method in derived class can use an uninitialized instance variable.
Welcome to the Ranch! And you are completely right - it is very dangerous to do this. If you ever choose to do this you must document it so people implementing sub classes know that they cannot use any instance variables. Better is to avoid it altogether.
Note that Sun ignores both advices. They call several methods in constructors (for example setModel in several Swing components) without mentioning this.
YogeshS Pandit
Greenhorn
Joined: May 10, 2010
Posts: 9
posted
0
Thanks for confirming Rob.
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32689
4
posted
0
Rob Prime wrote: . . . Note that Sun ignores both advices. They call several methods in constructors (for example setModel in several Swing components) without mentioning this.
Was that before people realised what a bad idea it is to call methods from constructors?
If it was then people have not though things through early enough
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32689
4
posted
0
There are lots of things hiding in the API which people obviously didn't think about in time.
Neil Cartmell
Ranch Hand
Joined: Feb 13, 2010
Posts: 150
posted
0
Campbell Ritchie wrote:
Rob Prime wrote: . . . Note that Sun ignores both advices. They call several methods in constructors (for example setModel in several Swing components) without mentioning this.
Was that before people realised what a bad idea it is to call methods from constructors?
Wait a minute. It's a bad idea to call methods from constructors? I've just started learning about how to make a GUI and the book i'm reading tells you to call LOADS of methods from the constructor of the JFrame class. Like all the add button methods. Is this something i should avoid?