• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

doubt in class hierarchy

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fellas !! I have a doubt in class Hierarchy!! Please Help me !!



Here we are supposed to find at which line an exception is thrown
I think it should be line 14 but thats not the answer. Thats because if it would have been B b=new C(); then we could have written b=(C)m but the line is B b=new B();
This example is taken from Devaka Coorey !!

 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code is correct.

B b=new B();
Min m=null;
b=(C)m; // 1

At line // 1: b is a reference of type B, and you are trying to assign it a reference of type C (which points to null.) Since C is a subtype of B, this will not be a problem.
 
reply
    Bookmark Topic Watch Topic
  • New Topic