• 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

please help

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Trebble{
int i = 99;
}
class Base extends Trebble{
int i =100;
}
public class Central extends Base{
public static void main(String argv[]){
Central c = new Central();
c.wynyard();
}
public void wynyard(){
Trebble t = new Central();
Base b = (Base) t;
System.out.println(b.i);
}

}



class Base {}
class Sub extends Base {}
class Sub2 extends Base {}
public class test extends Sub{
public static void main(String argv[]){
Base b=new Base();
Sub s=(Sub) b;
}
}
(one compiles fine but other gets a runtime error)
can u please explain these two programs output.thanks
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

output is 100.

Instance variables binded at compile time depending on the type of the reference variable and not on the object.
At compile time, b has Base type.



b is a instance of Base class. You can not cast it to its subclass.
 
srilatha annepu
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic