This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Casting Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Casting Doubt" Watch "Casting Doubt" New topic

Casting Doubt

Gitesh Ramchandani
Ranch Hand

Joined: Feb 28, 2007
Posts: 274

Output is: in SUB

Doubt: why the output is in SUB, even when I�m casting it to super class?
[ January 11, 2008: Message edited by: Gitesh Ramchandani ]
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42958
Please QuoteYourSources.
Gitesh Ramchandani
Ranch Hand

Joined: Feb 28, 2007
Posts: 274
Source: Self Doubt
Sudhakar Duraiswamy
Ranch Hand

Joined: Dec 20, 2007
Posts: 43
hi Gitesh ,

Inside child
classname =main Class

SCJP 5.0 93%| SCWCD (prep. ongoing))
gurpreet singh
Ranch Hand

Joined: Apr 07, 2006
Posts: 38
It is because the "start()" method is overriden in class tt. Remember, Invocation of instance methods are determined by the object referred to at runtime. So at runtime it is Class tt object.

If you use the syntax "((t)new tt()).variable", then it will invoke the variable of superclass and not that of subclass. Because the invocation of variables is determined at compile time and not at runtime.

Gitesh Ramchandani
Ranch Hand

Joined: Feb 28, 2007
Posts: 274
Thanks Sudhakar.

I get it now that overriding method will be invoked and decided during runtime based on the object type, rather than the reference type.
Dean Jones
Ranch Hand

Joined: Dec 29, 2007
Posts: 129
Is it becoz polymorphism only applies to instance methods?
Have you checked out Aspose?
subject: Casting Doubt
It's not a secret anymore!