Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

scjp5.0 question from 2nd chapter

 
nikunj thakur
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Dog { }
2. class Beagle extends Dog { }
3.
4. class Kennel {
5. public static void main(String [] arfs) {
6. Beagle bl = new Beagle();
7. Dog dogl = new Dog();
8. Dog dog2 = bl;
9. // insert code here
10. } }

Which, inserted at line 9, will compile? (Choose all that apply.)

Beagle b2 = (Beagle) dog1;

Beagle b3 = (Beagle) dog2;

Beagle b4 = dog2;

None of the above statements will compile.


The answer is 1st & 2nd line,but when i run the program it showing error
for the 1st answer
 
Praveen Seluka
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

KB says the first two line would compile but the firstline would give a runtime exception.Check it out.
 
P Ventura
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First line compile because all assignment and casts are correct, but throw a runtime exception: the actual object is not a Beagle.

Second line compile and run correctly.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic