• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Polymorphism

 
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 247
MyEclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for confirming Rob.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If it was then people have not though things through early enough
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are lots of things hiding in the API which people obviously didn't think about in time.
 
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Neil Cartmell wrote:ok thanks!


you are welcome
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic