• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt regarding the implementation class!!

 
Subhashish Pattnaik
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all, i had come across this particular question which has had me bugging all the day. Kindly help me out!!


Q. Which of the following code snippet when inserted individual at the commented line, will cause a java.lang.ClassCaseException?
The options are
(1) Greek f =(Beta)(Alpha)x;
(2) Alpha a = x;
(3) Greek f = (Alpha) x;
(4) Beta b = (Beta)(Alpha)x;
(5) Greek f = (Delta)x;

Would request you to kindly elucidate your solution!!

Many thanks

Subhashish
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12017
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happened when you inserted each line, one at a time, then compiled and ran it?
 
Subhashish Pattnaik
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the first 4 options, the program compiles adn runs without showing any exception..!!

But thats not the case for the 5th option where the stance is given by

Greek f = (Delta)x;
This runs without returning any compliation eroor but returns the java.lang.ClassCastException during the runtime which states Beta cannot be cast to Delta. Hence 5th option is the solution.

But am not clear with how the stuff has worked out to be, specially with reference to the first and fourth option!!
Need your help please

Regards
Subhashish


 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12017
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok...

on line 11, where the actual object is created, we make a Beta object. That is what's most important here - we have a Beta object.

then, still on line 11, we assign it to a Beta reference. So far, so good.

Assume we put in option #5: Greek f = (Delta)x;

'x' is a Beta reference, which conceivable COULD actually point to a Delta object, since Delta is a sub-class, so the cast is legal. We can assign that to a Greek reference, since Delta extends Beta extends Alpha which implements Greek - any Delta object is a Greek.

However, when we run things, we have a problem. When we try and do the cast of x to Delta, the ACTUAL OBJECT is a Beta. If the cast were allowed, then the Delta reference could try and call methods that aren't defined in the object. It's not until we have actual objects we can tell if the cast is allowed, since the cast is POSSIBLE.

If we were trying to cast a Beta to a ArrayList, the compiler know that can never happen, so it would be disallowed.



in option 1: Greek f =(Beta)(Alpha)x;

again, we have a Beta object. ALL Beta objects are Alpha objects, so the (Alpha)x cast is allowed. Then, since it is possible that a Alpha could be a Beta, the compiler allows this. Then, since all Betas are Greeks, we can assign it to that reference.

I believe that again, if line 11 were something like String x = new String("hello"), the compiler would complain. If it were Alpha x = new Alpha(), the compiler would allow it (again, it's POSSIBLE to down-cast from Alpha to Beta), but you'd get a run-time error.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Subhashish, welcome to JavaRanch.

We have a rule here: when you copy a question from a book, website, mock exam or other source, we require that you tell us where you copied it from - QuoteYourSources.

Please tell us where you copied this question from.
 
Subhashish Pattnaik
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aha!! Many a thanks for the explaination..!! this has got down well. But i guess, i'll have to come back to you once again

As for your second query boss, i just got the question from my company's previous questionnaires that were put forth us.

Regards
Subhashish
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic