Win a copy of Spark in Action this week in the Open Source Projects 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 an Object twice in one statement

 
Ranch Hand
Posts: 113
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

From one of Enthuware's mock exams:

Consider the following classes :


And the following declarations:


Identify options that will compile and run without error.

The answer and explanation is as follows:

a = (B)(I)b;
class B does implement I because it extends A, which implements I. A reference of type I can be cast to any class at compile time. Since B is-a A, it can be assigned to a.



Doesn't (B)(I)b essentially downcast? First b is upcasted to I, which is fine, but then it's downcasted to B, which should be a problem. Or does downcasting only focus on the object type that the reference variable is referring to (which is B)? In other words: as long as b is not casted to C at any moment (in this example), no downcasting is actually happening?

Thanks again.

Regards,
Shane
 
author
Posts: 23877
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Shane Jensen wrote:
Doesn't (B)(I)b essentially downcast? First b is upcasted to I, which is fine, but then it's downcasted to B, which should be a problem. Or does downcasting only focus on the object type that the reference variable is referring to (which is B)? In other words: as long as b is not casted to C at any moment (in this example), no downcasting is actually happening?



I think that it may be best to think of it this way... A cast does *not* change the type of the object itself. It is just how it is accessed, either via assigned to a reference, or as a temporary reference in the expression. So, as long as the object IS-A type, of the specified casts, then it should compile.

Henry
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Same Enthuware question discussed in this excellent topic.
 
There is no beard big enough to make me comfortable enough with my masculinity to wear pink. Tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic