• Post Reply Bookmark Topic Watch Topic
  • New Topic

What is the reason behind this!!!  RSS feed

 
shesh anand
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int i;

byte bt = (byte)i;/*ok here, because if we put bigger bucket into smaller
bucket we have to do this*/


BUT

why the thing that's happening below happens the way it does

subClassObject = superClassObject;

but, superClassObject = (superClass)subClassObject;

i am a bit confused ain't i...please help me out
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by shesh anand:
...why the thing that's happening below happens the way it does

subClassObject = superClassObject;

but, superClassObject = (superClass)subClassObject;...

Actually, it's the other way around -- so maybe that explains the confusion.

In all cases, the subtype IS-A supertype. So you can always assign a reference to a supertype without an explicit cast, because "upcasting" is always safe.

superClassObject = subClassObject;

But a supertype is NOT always a subtype. For example, a Car reference might point to the subtype Volkswagen, or it might not. So an explicit cast is required for "downcasting."

subClassObject = (subClassObject)superClassObject;
 
Bob Ruth
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The subject matter that will explain that is conversion (implicit versus explicit) and casting.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!