• 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

help on these two codes

 
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
//anybody tell me why this code throw runtime exception???

Question 1






(nothing is urgent in SCJP)
[ November 26, 2005: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q.1 :

Derived is subclass of Base class. Unless Base reference points to a Derived object, you cannot type cast a Base reference to Derived reference.

For eg. if you change your code to
Base objBase = new Derived();
Derived objDer = (Base) objBase;

then you would not face any runtime exception. That is because extends means is-a. Consider a class Animal, which is extended by two other classes Dog and Horse. Both Dog object and Horse Object is an Animal. That is why at runtime, unless the Animal reference was earlier pointing to Dog, it cannot be typecasted to a Dog reference. Hope my explanation helps.

Q.2) You can access by n.i; and not Balach2.n.i; You can access static member variables with the class name. In this example n is not a member variable at all. It is a reference present in the main method.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic