Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Casting Classes

 
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is a following question from DanC...


What is the result of attempting to compile and run the above program?

the answer is that it compiles fine. In the explanation, Dan writes...
"Although line 12 appears to be the source of a compiler error it is not. In line 12, the reference named base actually refers to an object of type Sub. Therefore, the reference to the object may be cast to type Sub. "
Now, if base was actually an object of type Base, i thought it might stil be okay, as there is an explicit cast to the type of Sub?
[Dan added the UBB codes to format the code block.]
[ March 07, 2003: Message edited by: Dan Chisholm ]
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Now, if base was actually an object of type Base, i thought it might stil be okay, as there is an explicit cast to the type of Sub?


Sorry Boss. You can't climb the inheritance higherachy tree upwards. Only you can roll down...
Compile time it is allowed but you get run time error.
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Sarma Lolla:

Sorry Boss. ...Compile time it is allowed but you get run time error.



aha, yes, thankyou JBoss!
i thought it might be that, compile fine, but runtime error.
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so...the only reason for the explicit cast is because... um, well, i guess, maybe it isn't required? No, it probably is, because although base refers to an obj of type Sub, the actual reference variable is of type Base?
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Jasper,
i think the cast is required if you want to access the attributes of the object fully.
if you didnt use the cast, then you can't use the additional attributes or methods in Sub.
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Mohammed!
i guess the reason for that is that polymorphism only applies to overridden methods - it does not apply to overloaded methods?
[ March 08, 2003: Message edited by: Jasper Vader ]
 
no wonder he is so sad, he hasn't seen this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic