File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes default class accessing another default class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "default class accessing another default class" Watch "default class accessing another default class" New topic
Author

default class accessing another default class

Billal Rabah
Greenhorn

Joined: Dec 04, 2007
Posts: 4
Hi,
Please check out the following code:
// TestAnimal.java
package com.mycompany.animals;
class Animal{}
class Horse{}
public class TestAnimal{}

// Canine.java
package com.mycompany.animals;
public class Canine extends Animal{}

when i tried to compile Canine.java a compiler gave me an error that a class Animal is not found. I thought since both Canine and Animal in the same package then Canine can access( extends in this case) Animal since it has a default access.
Let me know what i am missing
and thanks
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

"Java Man,"

Welcome to JavaRanch! Please check your private messages by clicking on My Private Messages. Thanks!


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

I don't think it's an access issue.

Have you already compiled TestAnimal, so that the Animal class file exists? Or are you expecting javac to figure out that the class definition is in a file with a different name?

(If the Animal.class file does exist, then it's probably a classpath issue. Let us know...)
[ December 04, 2007: Message edited by: marc weber ]
Billal Rabah
Greenhorn

Joined: Dec 04, 2007
Posts: 4
Yes, I have compiled TestAnimal.java and i have all the three classes(TestAnimal, Animal, Horse)
Billal Rabah
Greenhorn

Joined: Dec 04, 2007
Posts: 4
Here are the commmands
D:\Documents and Settings\GG\My Documents\My Java\source>javac -d ../classes com/mycompany/animals/TestAnimal.java

D:\Documents and Settings\GG\My Documents\My Java\source>javac -d ../classes com/mycompany/animals/Canine.java
com/mycompany/animals/Canine.java:5: cannot find symbol
symbol: class Animal
public class Canine extends Animal{}
^
1 error

D:\Documents and Settings\GG\My Documents\My Java\source>
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

It looks like...
  • The directory "My Java" contains (at least) 2 subdirectories: "source" and "classes".
  • The current directory (the one you're compiling from) is "source".
  • "com.mycompany.animals.Animal.class" is under the directory "classes".
  • So you need to specify a classpath so that javac can find com.mycompany.animals.Animal.class. The flag for classpath is -cp (similar to the -d directory flag you're already using). So from your current directory, do you see what the classpath value should be?
    [ December 04, 2007: Message edited by: marc weber ]
    Billal Rabah
    Greenhorn

    Joined: Dec 04, 2007
    Posts: 4
    Thanks the code runs great now. Yes, it was a CLASSPATH problem. I have used the flag -cp as you instructed.
    Before
    D:\Documents and Settings\GG\My Documents\My Java\source>javac -d ../classes com/mycompany/animals/Canine.java
    com/mycompany/animals/Canine.java:5: cannot find symbol
    symbol: class Animal
    public class Canine extends Animal{}
    ^
    1 error
    After
    D:\Documents and Settings\GG\My Documents\My Java\source>javac -d ../classes -cp ../classes com/mycompany/animals/Canine.java

    D:\Documents and Settings\GG\My Documents\My Java\source>

    Appreciate it man
    marc weber
    Sheriff

    Joined: Aug 31, 2004
    Posts: 11343

    Glad it works!
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: default class accessing another default class
     
    Similar Threads
    static method can't be overridden but this code is not?
    why don't protected inner class compile?
    Best Practices (Static Methods with OOP)
    access specifier protected
    abstract classes/methods