Forums Register Login

how to access a class within a package?

+Pie Number of slices to send: Send
Off the diretory D: i have created a package named 'practise' which contains a subpackage named 'food' inside which there is a class named as Fruit which is public..
Now i have created another class named Apple which is default and is present in the package 'practise' and extends Fruit...

how do i set the classpath, compile and run the program???
+Pie Number of slices to send: Send
Welcome to JavaRanch

Don't set the classpath at all.
So you have?
Create a folder to hold your Java work and put all your work in that, rather than the root of the D drive. Otherwise you are liable to get your D drive cluttered and confused.

You need another class in the practise package which calls the Apple classPut your Fruit, Apple and AppleDemo .java files into your java folder. No need to create any other folders yet.There are several other ways to do it, but that method will work.

The -d option creates any directories required, and the . tells the javac tool to look in the current directory for the ".java" files. You can find other ways to do it by searching this forum, and this thread is one of the things you find. Beware: I made a mistake in that thread, and somebody else noticed it.
+Pie Number of slices to send: Send
thanks for the welcome and speedy reply.....i tried what you said but unfortunately it didnt work out....can you elaborate a bit more or shed light on some other ways to the problem
+Pie Number of slices to send: Send
 

Faisal Ansari wrote:i tried what you said but unfortunately it didnt work out....


Please explain what you did and why it didn't work, because just saying "it didn't work" is not very useful and doesn't help us to help you solve the problem.

Have a look at Sun's Java Tutorials: Creating and Using Packages
+Pie Number of slices to send: Send
ok the question i have been trying to solve is as follows :

1) Create the superclass as follows :
package food;
public abstract class Fruit{/* any code you want*/}

2) Create the subclass as follows in a different file:
import food.Fruit;
class Apple extends Fruit{/* any code you want*/}

3) Create a directory called 'food' off the directory in your class path setting

4) Compile and run

how do i go about it?
+Pie Number of slices to send: Send
i forgot to mention that the Apple class isnt placed in any package...
+Pie Number of slices to send: Send
Faisal, one thing to keep in mind is that a class's name is actually the fully qualified name -- i.e., the package and the class name. So your classes aren't simply Apple and Fruit, but practise.Apple and practise.food.Fruit. The location of those classes is the directory where that package structure starts. In your case, it's the root of your D drive. In other words, both practise.Apple and practise.food.Fruit are at at the same location. In this case, if you need to set classpath to point at your classes, you would point to D. If you were to code Apple on your C drive, C:\practise\Apple, and Fruit on your D drive, D:\practise\food\Fruit, then your classpath would specify two locations, D and C.

I'm just pointing this out because beginners often think of a class name as separate from its package name, which leads to confusion about imports and classpath. I'm also pointing it out to illustrate that classpath points to the directory of where your package.Class is defined, not to the actual file location of the class itself.

(Note that you would not normally split your package into two folders or directories, like I did above when I put Apple and Fruit on the C and D drives. It's allowed, but I only did that to illustrate how the classpath would be affected. In practice, separating your source code like that would be confusing. However, I do normally separate my jUnit tests into their own source folder, but that's another topic.)
+Pie Number of slices to send: Send
 

Faisal Ansari wrote:i forgot to mention that the Apple class isnt placed in any package...


? Perhaps you could post your Apple and Fruit source files.
+Pie Number of slices to send: Send
thanks max i got it
+Pie Number of slices to send: Send


+Pie Number of slices to send: Send
Ah. I see that your classes are in package "food" and the default package. So your classpath would be set to whatever folder holds food.Fruit and Apple.

Note that most development environments help manage classpath for you. As a beginner, it may be best to do these things manually to better learn about what's going on. Soon, setting classpath becomes a hassle, and you'll probably want to switch to a more sophisticated development environment.

If you're running on Windows, then you can use Notepad and the command line for compiling and running your code. Or use Textpad, which is slightly more convenient, since you can run javac from within the editor, yet doesn't help too much with regard to classpath. Later you can switch to something like Eclipse.
+Pie Number of slices to send: Send
thanks max....but how to set the classpath is only the thing i need to know....can you help me
+Pie Number of slices to send: Send
From the command line, navigate to the folder where "Apple.java" is coded and type: javac Apple.java. This sets classpath to its default, and looks in the current folder for the classes Apple references.

If you were one folder up, and your code is in folder "practise", then you'd type: javac -classpath practice/ practice/Apple.java. In this case, -classpath says "look in the practice folder practise as the starting point to resolve class references".

(I'm in UNIX, so I use "/". If you're in Windows I guess you'd use "\".)
+Pie Number of slices to send: Send
thanks max....i got it
It will give me the powers of the gods. Not bad for a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 2114 times.
Similar Threads
Creating an Abstract Superclass and Concrete Subclass
Default Package
Problem with packages
package problem
Object Creation in inheritance
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 08:30:59.