Hi I'm new to programming so I need a little help with packages. I have created 2 packages test1 and test2 with p1.java and p2.java respectively. Both have default access.
I'm trying to extend p1 with p2 and have tried to import test1 but the error im getting is that I there is no package called test2 and there is no class called p1. I have also changed the access to public but it is not helpin.
Thnaks in Advance

You can extend a class from a package but you can't extend the package itself
and you will have to show your directory structure and how you do your import statements

Are the both packages in the same sourse package?
They just can't
That is precisely why packages were invented

ok the source package contains test1 and test2. I'm not trying to extend the package.

package test1;
class p1{
//code here
}

package test2;
import test1.p1;
class p2 extends p1{
//code here
}


This is how the programs look

make class p1 public ...............i.e

package test1;
public class p1{
//code here
}

i tried making it public but it is not recognising the package at all. also the class

Under which directory did you put the .java files as well as the .class files. I mean that for example you should do something like this:

Folder : src/test1
File: p1.java

Folder : src/test2
File: p2.java

-----------
Folder : folder path/comp/test1
File: p1.class

Folder : folder path/comp/test2
File: p2.class

CLASSPATH = folder path/comp

It doesn't have to be exactly like what I mentioned above. But the folders in which the java files are inside must have the name of their package. p1 class that has package test1 must be under a test1 folder.

If you already do that, then you need to provide more information about what you are doing and what error do you get

I've done all that......in source folder.........there are 2 folders test1 and test2
test1 contains p1.java and test2 contains p2.java.

//This is in p1
package test1;
class p1{
//code here
}

//This is in p2
package test2;
import test1.p1;
class p2 extends p1{
//code here
}

Now p1 is compiling byt when i try to compile p2 it says that there is no package called test1 at all and that there is not a class named p1. Actually I'm supposed to get that its cant access because the declaration is default. But it is neither recognizing the package structure or the class p1.

friend follow the steps.................

open comand prompt
C:\
1.first create a folder(any name)................say X(within any directory say C)
C:\md X
C:\cd X
C:\X\
2.In X create to files i.e p1.java and p2.java.(class p1 should be public)
3.Now first compile p1.java ( using command javac -d . p1.java)
C:\X\javac -d . p1.java
4.Now compile p2.java ( using javac -d . p2.java)
C:\X\javac -d . p2.java
5. then run java test2.p2
C:\X\java test1.p2

the p1.class was created in test1...yes i did set the cpath

Then post some more information. What error do you get, where are the .java files are and where are the .class files where created as well as how did you set the classpath.

ok.....on the desktop i created a folder i set the class path......in the folder i created 2 sub folders test1 and test2.....u know the rest.....pi.class is in test1.....p2.class cant be created because it is not compiling....The error that im getting while compiling says that there is no package called test1 and no class called p1.

i tried making it public but it is not recognising the package at all. also the class

You need to compile the classes in the package using javac command. Befor compiling a class that imports another class in the package, you must compile the class it imports first. The reason being your compiler could not recognize the classes in your package, its because those classes were not properly compiled. Just follow [QOUTE]Shanki87[/QOUTE] steps.

This article has been dead for over six months. Start a new discussion instead.