aspose file tools*
The moose likes Beginning Java and the fly likes Polymorphism Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Polymorphism" Watch "Polymorphism" New topic
Author

Polymorphism

Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 418
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
Sheriff

Joined: Sep 28, 2004
Posts: 18842
    
  40


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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 418
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
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.


SCJP 5.0
Raza Mohd
Ranch Hand

Joined: Jan 20, 2010
Posts: 247

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.>
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
YogeshS Pandit
Greenhorn

Joined: May 10, 2010
Posts: 9
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

Joined: Oct 27, 2005
Posts: 19696
    
  20

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
Thanks for confirming Rob.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
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

Joined: Oct 27, 2005
Posts: 19696
    
  20

If it was then people have not though things through early enough
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
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
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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Feb 13, 2010
Posts: 150
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

Joined: Jan 28, 2008
Posts: 5575

Neil Cartmell wrote:ok thanks!

you are welcome
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Polymorphism