wood burning stoves 2.0*
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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Casting Doubt" Watch "Casting Doubt" New topic
Author

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
Marshal

Joined: Mar 22, 2005
Posts: 41155
    
  45
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


Sudhakar
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.


SCJP
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Casting Doubt
 
Similar Threads
Overriding Doubt
A simple Q but still have doubt
Doubt in the code
Output Doubt
Doubt with Inheritence