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.

hndr
  • 757
  • 13
  • 29
  • Seems perfect to define an `interface`. – Suresh Atta Oct 29 '13 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? – hndr Oct 29 '13 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. – JB Nizet Oct 29 '13 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. – Tim S. Oct 29 '13 at 12:23
  • @JBNizet, basically, I need something like [this][http://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. – hndr Oct 29 '13 at 12:26
  • @TimS. I do need the method to be in class. – hndr Oct 29 '13 at 12:27
  • But why can't you inherit from a base class? Without any concrete code example, it's quite hard to tell you how to refactor it. – JB Nizet Oct 29 '13 at 12:28
  • I was editing the question to add some code example just now, but I think I figured out how to do it from MadConan's answer. Thanks. – hndr Oct 29 '13 at 12:38

4 Answers4

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 {
 }
MadConan
  • 3,749
  • 1
  • 16
  • 27
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?

Yegoshin Maxim
  • 872
  • 2
  • 21
  • 54
0

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

agad
  • 2,192
  • 1
  • 20
  • 32
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.

QYuan
  • 1
  • 2