aspose file tools*
The moose likes Beginning Java and the fly likes how to make two classes in a packahe where one class extends the other Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to make two classes in a packahe where one class extends the other" Watch "how to make two classes in a packahe where one class extends the other" New topic
Author

how to make two classes in a packahe where one class extends the other

budsy remo
Ranch Hand

Joined: Sep 20, 2008
Posts: 103
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.
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
I m trying to run this code which involves three classes

Why don't you post the code that you already have and explain what problems you are having.
budsy remo
Ranch Hand

Joined: Sep 20, 2008
Posts: 103



This is one of the two classes in package


The class that should extend Employee and should be in the same package:-


Finally the class that should use the whole package

Thanks for the quick response by the way
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14111
    
  16

Your code looks OK. Do you still have a problem with it, or is it solved now?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
budsy remo
Ranch Hand

Joined: Sep 20, 2008
Posts: 103
yes i do . When i run the manager.java file it is not able to find the Employee class . How do i do that?
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
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,
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
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.


Thanks and Regards
 
Consider Paul's rocket mass heater.
 
subject: how to make two classes in a packahe where one class extends the other