This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach 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

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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: 41034
Please QuoteYourSources.

Ping & DNS - my free Android networking tools app
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?
Don't get me started about those stupid light bulbs.
subject: Casting Doubt
Similar Threads
Overriding Doubt
A simple Q but still have doubt
Doubt in the code
Output Doubt
Doubt with Inheritence