• Post Reply Bookmark Topic Watch Topic
  • New Topic

Want to understand explicit type casting in Objects  RSS feed

 
Saed Hussein
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have come across on OCA Study Guide by Jeane Boyarsky & Scott Selikoff;
I have been reading the polymorphism section.
I have been fully understanding up-casting and down-casting in primitives data types. if I try to apply my previous experience in type casting, which class is greater then the other if they are superclass and it's subclasses or interface and implementing abstracts & classes. and how some methods would not available in some objects.
I apologize in advance if my English seems poor
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As for “greater” and “smaller”: I would suggest you look in the Java┬« Language Specification (=JLS) and see what operators they use. You probably won't find the JLS easy to understand.

As you doubtless already know, casting to a supertype always works without risk of throwing an Exception. But there is hardly ever a need to cast upwards.You would not notice the difference if you take the cast away. Casting down is different; you cannot change the type of an object, but you can tell the compiler that it will be a particular type. The compiler will either see the cast is impossible…or see the cast is not impossible and it will then believe you implicitly until the runtime complains with an Exception.Down casts may be required, but they are risky and error‑prone and you should design your classes to avoid any need to cast downwards.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!