Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
budsy remo
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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
Posts: 15272
37
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code looks OK. Do you still have a problem with it, or is it solved now?
 
budsy remo
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic