Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

packages

 
Alan Phillips
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,
I can't seem to get my package members to recognize each other. I have to files as follows:


and

The files are both saved in a directory called \java\interfacetest. Both \java and \interfacetest are included in the classpath. I only have one j2sdk installed too. I'm just doing some testing so I can understand casting to interfaces for the scjp, but I can't get it to compile! Here are the errors:
C:\java\interfacetest\tree.java:2: package interfacetest does not exist
import interfacetest.branch;
^
C:\java\interfacetest\tree.java:4: cannot resolve symbol
symbol : class branch
location: class java.interfacetest.tree
public class tree implements branch
^
C:\java\interfacetest\tree.java:6: cannot resolve symbol
symbol : class branch
location: class java.interfacetest.tree
public static branch b;
^
3 errors
Thanks!
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try putting your packages in a bit more logical order. Right now, I think you structure looks like this:

try reorganizing things like this (it's too confusing to have two packages names the same thing in different locations):

2nd thing, you might want to rethink your architecture (Aggregation vs Inheritance).
Basically the "has-a" vs the "is-a" relationship. A tree has-a branch (aggregation). You can't say that: A tree is-a branch (inheritance) so it doesn't really make sense implement the branch interface.
What if you did something like this?
interface tree
class deciduous implements tree
class evergreen implements tree
class oak extends deciduous
class pine extends evergreen
class spruce extends evergreen
then those classes could contain objects of type branch, type leaf or type needle etc.
 
Alan Phillips
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Yeah, I didn't really plan out the names. My goal was to test if an interface took on the members of a class by declaring an interface and then assigning it to a class. What I meant to set up was
package interfacetest
/ \
/ \
tree(class) branch(interface)
and have the tree implement the branch. But it can't find it. Am i doing something wrong with the package declaration? Good point with the sloppy naming though.
 
Alan Phillips
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, the above didn't come out the way I wanted. I want it to look exactly like your second example!
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You do not need the "java." in the package statement in the second code snippet. BTW you should not use "java", "javax", and a host of other prefixes, because they are considered reserved by Sun.
If your two files have the same package statement, here "package interfacetest", then you do not need the import statements.
Be aware that there are requirements on the names of the directories in which the files reside and your classpath must be defined correctly, so to find theses directories.
-Barry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic