This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes method in subclass not seeing method in abstract superclass Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "method in subclass not seeing method in abstract superclass" Watch "method in subclass not seeing method in abstract superclass" New topic
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
    
    6

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
    
    1

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.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel/download
 
subject: method in subclass not seeing method in abstract superclass
 
Similar Threads
how to access a class within a package?
i am getting identifier expected error in line 4... can any one help me out
Real Time Scenerio for abstract class and interface
The 'DateFormat' abstract class
Creating Objects