Can I have same class name on different package ?

K

krislioe

Hi,

I create two public class with same name, in different package, but it
gets error when compiled.

package test.one;
public class myclass1 {
public static void main(String[] args) {
System.out.println("myclass1");
}
}

package test.two;
import test.one.myclass1;
public class myclass1 extends test.one.myclass1{
public static void main(String[] args) {
System.out.println("inherit myclass1");
}
}

Error(3,14): class myclass1 clashes with imported class
test.one.myclass1

Is there aniway I can have same class name on the above case ?

Thank you,
xto
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

I create two public class with same name, in different package, but it
gets error when compiled.

It should work fine.
package test.one;
public class myclass1 {
public static void main(String[] args) {
System.out.println("myclass1");
}
}

package test.two;
import test.one.myclass1;
public class myclass1 extends test.one.myclass1{
public static void main(String[] args) {
System.out.println("inherit myclass1");
}
}

Error(3,14): class myclass1 clashes with imported class
test.one.myclass1

When you do:

import test.one.myclass1;

you make myclass1 mean test.one.myclass1, but
myclass1 could also mean myclass1 in current package
that is test.two.myclass1 !

Drop the import.

Arne
 
P

Patricia Shanahan

Hi,

I create two public class with same name, in different package, but it
gets error when compiled.

package test.one;
public class myclass1 {
public static void main(String[] args) {
System.out.println("myclass1");
}
}

package test.two;
import test.one.myclass1;
public class myclass1 extends test.one.myclass1{
public static void main(String[] args) {
System.out.println("inherit myclass1");
}
}

Error(3,14): class myclass1 clashes with imported class
test.one.myclass1

Is there aniway I can have same class name on the above case ?

The problem is that you are importing test.one.myclass1. Get rid of the
import, and just depend on fully qualifying the name of the other
myclass1, which you are already doing.

Patricia
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,271
Latest member
BuyAtenaLabsCBD

Latest Threads

Top