• 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 again

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to understand something
class Father {
int var1=10;
void amethod() {
System.out.println("father method ");
}
}
class Son extends Father {
int var1=20;
void amethod() {
System.out.println("father is over ridden ");
}
void bmethod() {
System.out.println("son's method ");
}
public static void main(String as[]) {
Father f=new Son();
System.out.println(f.var1);
//f.bmethod();
f.amethod();
}
}
I got the errorwhen I call f.bmethod(); why is that if f.amethod() access the Son class amethod() then why I can not access bmethod() by the same way???
I appreciate help
Thanx
Jaffery
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

why is that if f.amethod() access the Son class amethod() then why I can not access bmethod() by the same way???


The reason you get a compile time error is that you've declared f to be of type Father. Even though f "really" points to a Son object at run time, the complier doesn't know this. All the compiler knows is that f is declared to refer to a Father object and that a Father object doesn't have a method called bmethod(). That's why you get a compile time error.
You might benefit from reading this page:
How my Dog learned Polymorphism
Let us know if you're still confused after reading that page.
 
Jaffery Rab
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanx much I got it now
by the way my cat is waiting to learn polymorphism>>>>>>>>>>>>>>>
Jaffery
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic