Avoiding code duplicacy java

Hi,

There are two classes A and B both extends a Class C

class A extends C
{
}

class B extends C
{
}

Now there are some common function which i want to use in class A as well as B and so i move them to class C to avoid code duplicity

There is a third class D which extends E and this class E is in a separate Jar
Now i need the common methods which i had put in C to be used in D too.

One way is to put it directly in D. but that would lead to code duplicity.
As E is in a separate jar the common code cannot be directly moved to E as the common code uses some classes in the
current project.

What way i can resolve this? Is there any way to avoid code duplicacy here and not to put the common functions in C directly under D ?

Thanks
Rohit BajajAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rohit BajajAuthor Commented:
Also on similar lines i encountered one more issue :

I have the following two classes :

GroupListFragment extends HomeListFragment
SearchFragment extends BaseListFragment
HomeListFragment extends BaseListFragment


GroupListFragment and SearchFragment both have a common function leaveGroup()

The BaseListFragment is in a separate jar. How do i avoid code duplicacy of the function leaveGroup() in both GroupListFragment and SearchFragment
0
dpearsonCommented:
There are a couple of approaches for this:

a) Add another base class to the hierarchy
    LeavableGroup extends BaseListFragment

    HomeListFragment extends LeavableGroup
    GroupListFragment extends HomeListFragment
    SearchFragment extends LeavableGroup

Then implement leaveGroup() in the LeavableGroup() class and it'll be available to the others.

b) Move the functionality for leaving a group to a separate class outside of the hierarchy
   public class Leave Group {
       public void leaveGroup(BaseListFragment fragment) {
              ....
        }
    }

and have each GroupListFragment and SearchFragment create a LeaveGroup class (as a data field) and call leaveGroup() on it.

For this to work, the LeaveGroup class needs to be able to see enough of the BaseListFragment to actually do the work of leaving the group - e.g. if there's a list and you need to take it off the list, it needs access to that list.

Hope that helps,

Doug
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zzynxSr. Software engineerCommented:
As E is in a separate jar the common code cannot be directly moved to E as the common code uses some classes in the current project.

If you have common functionality code, you should put it in a common jar/project/library.
That way it can be reused by each and every project.

GroupListFragment and SearchFragment both have a common function leaveGroup()
Then move that leaveGroup() method to the class both GroupListFragment and SearchFragment have in common: BaseListFragment.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.