Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Polymorphism

 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The output of below shown code is:

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.

Can anybody please clarify?
 
Henry Wong
author
Marshal
Pie
Posts: 21412
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Basically, polymorphism applies -- even before instantiation of the object has completed. Since the derived class overridden the base printme() method, it is always called.

Henry
 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Raza Mohd
Ranch Hand
Posts: 247
Java MyEclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raj Kumar Bindal wrote:I really doubt if polymorphism is applied before instance creation.

Really? Because everybody here, and your own example too, clearly state otherwise.
 
YogeshS Pandit
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for confirming Rob.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it was then people have not though things through early enough
 
Campbell Ritchie
Sheriff
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are lots of things hiding in the API which people obviously didn't think about in time.
 
Neil Cartmell
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neil Cartmell wrote: It's a bad idea to call methods from constructors?


the method *which is designed for overriding* by subclass . otherwise, there is no harm to call a method[say static method] from constructor
 
Neil Cartmell
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:
Neil Cartmell wrote: It's a bad idea to call methods from constructors?


the method *which is designed for overriding* by subclass . otherwise, there is no harm to call a method[say static method] from constructor


ok thanks!
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neil Cartmell wrote:ok thanks!

you are welcome
 
Campbell Ritchie
Sheriff
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote: . . . there is no harm to call a method[say static method] from constructor
Disagree. Probably best only to call private or final instance methods from a constructor.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic