Google+

Like Us On Facebook


Total Pageviews

211. Creating duplicate Classes in same project







Pre-requisites -


Can we create duplicate Classes in Same Java Project ?

No, Java gives compiler errors when you create duplicate classes (i.e. Classes with same names).

 Is there any solution to create duplicate Classes in the Same Java Project ?

Yes, we can create duplicate classes in the same Java Project by separating the duplicate classes using Packages.

Lets implement this concept on Eclipse IDE

1. Create a new Java Project 'Project 39' as shown below -



2. Create a Class with name 'ClassA' as shown below -


3. Try to create another Class with same name 'ClassA' and observe that an error is displayed and am unable to create the Class with the same name as shown below -



Hence we cant create duplicate Classes in a single Java Project. Now lets resolve this issue by separating the Duplicate Classes using Packages by following the below steps.

4. ClassA Class created in Step2 was created under a default package. Now lets create another package 'packageD' in the same Java Project 'Project 39' as shown below -


5. Now create a Class with same name ClassA under the newly created package 'packageD' and observe that no error is displayed and the Duplicate Class is successfully created as shown below -


Hence we can create duplicate classes, when we create separate the Classes using packages.






Please comment below to feedback or ask questions.

Default Package will be explained in the next post.




No comments:

Blog Index