Question

How does a type cast such as (BankAccount) x differ from a cast of number values...

How does a type cast such as (BankAccount) x differ from a cast of number values such as (int)x?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

In the context of interfaces, a cast is necessary to convert from an interface type to a class type.

If x refers to a a BankAccount that was implemented as a Measurable object, then casting that object back again to a BankAccount will work--if it doesn't, the program won't even compile.

If, on the other hand, x refers simply to a floating point number that we want to convert (cast) to an int, it will work, although we implicitly agree that it's okay for us to lose some information along the way

(the fractional part of the number in this case).

Add a comment
Know the answer?
Add Answer to:
How does a type cast such as (BankAccount) x differ from a cast of number values...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT