| Author |
method in subclass not seeing method in abstract superclass
|
Bud Tippins
Ranch Hand
Joined: Jan 28, 2011
Posts: 52
|
|
I have the following code in the main.java file.
In the abstract Fruit class I have:
Why am I getting an errot on the "myApple.sayFruit();". Isn't the sayFruit() method visible to main?
Thank you.
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9944
|
|
is that all your code? Your main() method has to be inside a class, which it does not appear to be from what you posted. I put this in a file called "Tester.java"
and this in another file called Fruit.java
I compiled both files, and it ran just fine:
C:\slop> java Tester
I'm gonna call apple
I'm an apple
I'm a fruit
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Paul Sturrock
Bartender
Joined: Apr 14, 2004
Posts: 10336
|
|
You've defined your sayFruit() method with the package-private modifier (i.e. you've not used an access modifier like public, private or protected for it) so visibility for it is classes in the same package as your Apple class. Sub classes cannot see it unless they are in the same package too.
|
JavaRanch FAQ HowToAskQuestionsOnJavaRanch
|
 |
Bud Tippins
Ranch Hand
Joined: Jan 28, 2011
Posts: 52
|
|
Sorry. Here is the complete code in Main.java:
This is in fruit.java:
Thank you for your help
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3791
|
|
That's because you haven't added an access modifier to sayFruit. And by default it gets package access, which means that it's only accessible to classes in the same package - which doesn't include Main. Add public and it will work.
Edit: sorry - missed that Paul had already said that.
|
 |
Bud Tippins
Ranch Hand
Joined: Jan 28, 2011
Posts: 52
|
|
|
thank you. That worked.
|
 |
 |
|
|
subject: method in subclass not seeing method in abstract superclass
|
|
|