Implementing an interface and extending a class of a different project

3.4k Views Asked by At

This doubt has been bugging me for quite sometime now. I'll try to present it in the form of following scenario.

Scenario -

Assume I have 2 different projects, Project-1 and Project-2, in the same Workspace in Eclipse-JUNO. Project-1 has an interface I & a class A in the package P, and Project-2 has a class B in the package Q. I want to know if I can have the class B (of Project-2) implement the interface I and extend the class A (both located in the package P) of Project-1. If yes, how? If no, what are the alternative ways?

Looking forward to understanding this scenario.

Thanks in advance!

4

There are 4 best solutions below

0
On BEST ANSWER

For extending class A of Project-1,first it should be accessible/visible outside its package(make it public) and second thing your Project-1 should be on the classpath of Project-2

0
On

Yes, you can, as long as Project-1 is on the classpath of Project-2.

0
On

Make your Project-2 depend on Project-1 in eclipse. And change Class B declaration in project 2.

This scenario is just like any other jar file in the class path. project-2 is including project-1 jars in its classpath and working on it.

0
On

Yes, its absolutely possible when you add Project1 in the build path of Project2.

This is how you can do it.

  1. Right click on Project2, select 'Properties'.
  2. Select 'Java Build Path' in the list, click on 'Projects' tab.
  3. Click 'Add' and select 'Project1' checkbox, click OK.
  4. It's done.

Now, you can import classes from Project1 using import statements.

Hope, it helps.