• 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

object casting

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all
class Base{}
hi all
class Sub extends Base{}
class Sub2 extends Base{}
public class CEx{
public static void main(String ar[]){
Base b = new Base();
Sub s = (Sub)b;// run time error in this line
}
}
pls explain why there is a runtime error?
thank you
saman
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(Sub) b
this makes the compiler happy because the compile type of "b" (Base) is a supertype of Sub. However because it is a downcast the "interpreter" must make another check at runtime. It must ensure that the object pointed by "b" is really an object of type "Sub" (or one of its subtypes)
If the last check were not carried out, an object of type Base could be referred by a variable of type Sub; this could lead to such object to receive a method call declared in type Sub but not in Base. That is an object could receive a message for which is not prepared for. Java is a type safe language. You cannot call methods on objects that do not correspond with its type.
The exact rules for casting are given in JLS 5.5 Casting Conversion
As a general guide, keep in mind that the compiler will complain if there is no hierarquical relationship between the compile type of the expression to be casted "b", and the type inside the cast operator "()" (*). Later at runtime, a check of the real type of the object pointed by the expression maybe needed.
(*) Say there there is no a father-son relationship but they are siblings.
 
reply
    Bookmark Topic Watch Topic
  • New Topic