Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Casting Doubt

 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please QuoteYourSources.
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Source: Self Doubt
 
Sudhakar Duraiswamy
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Gitesh ,


Inside child
classname =main Class
 
gurpreet singh
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it becoz polymorphism only applies to instance methods?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic