• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

java newbie: implementation using other file

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all

I was just wondering what am I missing when I tried to implement a class from another file. Here is the thing, I created two classes. Class A is the interface and class B implements class A. When I tried to compile it using java version 1.5, I get an error that points to the method the interface class has. To be exact, it says that: Can't Find Symbol. But when I tried to compile it using version 1.6, it works just fine. I tried searching the forum here but seems I need to try harder.

Please help me understand this. actual code below

File number 1

interface Foo {
void doStuff(int y);
}

File number 2
class FooImpl implements Foo{

public static void main(String[] args){
Foo f = new Foo();
System.out.println(f.doStuff(5));
}
}

Compiler Error: CAN'T FIND SYMBOL- FOO

thanks in advance


 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jay, welcome to the Java Ranch. Please use Code Tags when posting code.

Looking at your code, I don't think it should compile in any version of Java. Foo is an Interface and you're not allowed to instantiate Interfaces. You are allowed to declare the type of a reference variable as the Interface type but you have to instantiate it is a class that implements that interface, not the interface itself. Also, your FooImpl class doesn't override the doStuff(int y) method so the Interface contract isn't satisfied. Try reworking this out and posting your results.
 
Jay Ceazar Boado
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jarred,

Appreciate your reply.

I edited my code based from the interface rule. Here is the thing, when I inserted the interface in the same file, it compiles perfectly and return the explicit value.



but when you separate the interface and save it as another file, same error: Cannot find symbol- points to FOO. I tried it with 1.6 and works fine. both file are located in the same folder.

and you know what, the return value of the preceding code, for some reason, is: FooImpl@19821f

thanks
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Post separate code chunks for each file, the directory structure, and the classpath you're using to compile.

Not sure why you're upset about the output of the code; it seems fine.
 
Jarred Olson
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure why it can't find the Interface in a separate file... maybe try adding a package statement at the top of both files to explicitly state the package.

For the print statement resulting in: "FooImpl@19821f"
When you place an Object inside of System.out.println() it implicitly class the .toString() method. Since FooImpl doesn't override the Object implementation of toString(), it gives you that garbled piece of useless text. Try adding this to FooImpl and keeping your print statement the same:
 
Jay Ceazar Boado
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:Post separate code chunks for each file, the directory structure, and the classpath you're using to compile.

Not sure why you're upset about the output of the code; it seems fine.



Hi David

Thanks for your reply. I am a little upset, maybe because, as expected, it should return 5 right?

And by the way, I fixed the problem with the cannot find symbol. My current classpath does not have the directory of my working folder. I get it to work. Thanks thank thanks for reminding me :jumpingjoy:


But like what I previously mentioned, the return value is still not 5 as declared explicitly. :cry:
 
Jarred Olson
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jay... read the post above your last one. Also your doStuff() method isn't doing anything with the variable... you would need to set it's value to a instance variable and then read that from your implemented toString() if that's your desired result.
 
Jay Ceazar Boado
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jarred Olson wrote:Jay... read the post above your last one. Also your doStuff() method isn't doing anything with the variable... you would need to set it's value to a instance variable and then read that from your implemented toString() if that's your desired result.



Hi Jarred,

My bad. I already submitted my post when I saw your reply. This is noted though.

Thank you so much for all your help. I really appreciate it. Until next time..
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jay:

Your "doStuff" method takes a parameter of type int. But the body of that method is empty -- it doesn't do anything with the passed value. If you want to associate the passed value with the object, you'd need to define an instance field in FooImpl, such as:

But this discussion getting into the fundamentals of OO design and implementation in Java. I wouldn't bother with interfaces until you've played around with defining objects, with properties, and "accessor methods" for getting and updating those values, etc.
 
Jay Ceazar Boado
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Max.

I couldn't agree more. And I'm getting there.

Bytheway, tried your code, and it worked!

thanks so much.. this will surely help..
 
Shiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic