1

I have 2 different methods that needs to be repeated across my program, e.g. methodA, and methodB.

Some part of the program needs to call methodA, and some others need to call methodB, and some needs to call them both. The implementation of these methods are the same across the program.

How should I structure the classes and methods to reduce the repetition? First thing that came to my mind was that I wish Java supports multiple inheritance.

The second thing that I thought of was to put each of the methods in their own interface, and have the classes implement them as necessary, but the implementation of the methods are exactly the same across the program, so having to define the implementation in each class doesn't sounds like a good idea.

I am limited to Java 6. 7 if I can convince the others in my team, so the default methods implementation coming with Java 8 is not an option.

p.s. I realize the title of the question is a very bad one, but I can't think of an easy way to explain this problem. Please suggest better one if you have any.

8
  • Seems perfect to define an interface. Commented Oct 29, 2013 at 12:21
  • @sᴜʀᴇsʜᴀᴛᴛᴀ, yes, but the implementation of the methods are exactly the same across the classes. wouldn't defining an interface require me to re-define the methods in each class? Commented Oct 29, 2013 at 12:22
  • 1
    It's not very clear why you can't simply extend from a base class defining these two methods. Example code would help. Commented Oct 29, 2013 at 12:23
  • If the implementation doesn't change, you should use static methods. The method doesn't need to be in the class in order to use it from the class. Commented Oct 29, 2013 at 12:23
  • @JBNizet, basically, I need something like [this][stackoverflow.com/a/18286258/1253747], but I can't use Java 8. edit to your update: because as I mentioned above, some class need A, some need B, and some needs both. Commented Oct 29, 2013 at 12:26

4 Answers 4

2

Off the top of my head:

public interface Foo{ public Object processA(Object first); public Object processB(Ojbect second); } public abstract class AbstractFoo implements Foo{ protected AbstractFoo(){} public Object processA(Object o){ // do stuff return null; } public Object processB(Object o){ // do more stuff return null; } } public class ConcreteFoo extends AbstractFoo { } 
Sign up to request clarification or add additional context in comments.

Comments

0

If this is a part of refactoring your code, why can't you put your repeating code in a side(maybe private) methodC and call it from those both methods?

Comments

0

You should have 2 interfaces, but you can have one class implementing both interfaces.

Comments

0

That's depend on what type of program you are writing.If it is a web system,I think you should bulid these two,because layer is necessary.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.