Guys i have a problem . I m trying to run this code which involves three classes
1. two classes need to be public and in the same package.
2. one of those classes is extending the other.
3. the third class imports the package and uses those classes.
how do i achieve the first two targets ? Example would be appreciated.
I assume you are attempting to compile the files from the command line. If not please specify how you are compiling the code. Please post the commands that you use, the directory structure where your files are, and in which directory you run the compiler.
BTW, Java is case sensitive so you wouldn't compile manager.java. You would compile Manager.java,
I hope that you Manager and Employee classes are in a sub-directory named 'one'.and the EqualTest class file is in the current directory.
Basically when you invoke java from a directory, that particular directory must contain the class files which are in default package. And the other classes which are in packages must be contained in the same directory structure as is the package structure, starting with the current directory.
The other important point is that the directory which denotes the default package must be included in the classpath. This is necessary so that java has a means of knowing about from where to start searching the class files from. It will look for the class files as per the package structure, starting from the current directory
Let's say you have 3 class A,B and C.
class A is in the default package and hence its class file must be placed in the current directory i.e. the directory from where you run 'java'
class B is in package p1 and hence its class file must be placed in a subdirectory called p1 of the current directory.
class C is in package p1.p2 and hence its class file must be placed in a sub-sub directory called p1/p2 of the current directory.
Now let's say you have to run the class C. For this you need to include the current directory in the classpath. This ensures that java will start searching for your class file from the current directory and find it in p1/p2 folder which is same as the package of class C i.e. p1.p2 So the command would look like:
java -cp . p1.p2.C
Please note that these file placement restrictions do not apply to the .java files and you can place then wherever you want.