Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

Can I have same class name on different package ?

Thread view: 
krislioe@gmail.com - 25 Aug 2006 04:10 GMT
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
Arne Vajhøj - 25 Aug 2006 04:16 GMT
> 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 {
[quoted text clipped - 13 lines]
> 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
Patricia Shanahan - 25 Aug 2006 04:18 GMT
> Hi,
>
[quoted text clipped - 20 lines]
>
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.