• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

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
 
It's a pleasure to see superheros taking such an interest in science. And this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic