• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

default class accessing another default class

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Java Man,"

Welcome to JavaRanch! Please check your private messages by clicking on My Private Messages. Thanks!
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I have compiled TestAnimal.java and i have all the three classes(TestAnimal, Animal, Horse)
 
Billal Rabah
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
    Posts: 4
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 11343
    Mac Safari Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Glad it works!
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic