• 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 with compiler speak

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was just looking over some notes and I decided to run this example:
class Testit3 extends Private {

void methodA() {
System.out.println("Inside child");
}

public static void main (String args[]) {
Private p= new Testit3();
p.methodA();


}
}
class Private {

private double methodA() {
return 25.0d;

}
}
I got this compiler error:
Testit3.java:9: methodA() has private access in Private
p.methodA();
which I was not expecting nor due I clearly understand the wording. I understand this cast - ((Testit3)p).methodA(); - is needed to make the example work.

I was expecting a could not resolve error as the private methodA() is not picked up by the child. You get this error when you comment out or delete private methodA() in class Private. Thanks in advance.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I rearranged the example and replaced the confusing Private with the identifier Parent. Now we can see better what is going on.

Parent declares methodA to be private so that the main method in Testit3 cannot refer to methodA via the reference p.
Notice that if you remove the private, then you get another error. This is because the two methodA methods differ in return type, although they have the same name and argument list.
[ August 26, 2002: Message edited by: Barry Gaunt ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic