1

I need to implements various update methods for Observers in my project, to overload the Object argument with a specific type. So instead of using

public void update (Observable o, Object arg0){
   if(arg0 instanceof Class1) ....
   else if (arg0 instanceof Class2) ....

or a switch (which is a very 'C' way of working), I thought of using an interface that extends Observer and overloads the update method, like this

public interface ExtendedObserver extends Observer{
   public void update (Observable obj, Class1 cls);
   public void update (Observable obj, Class2 cls);
}

and then implement it into my Observers. Every different Observer will use a specific update method.

I wanted to know if it's a correct way of working, because

  1. I don't think overloading a method inside an interface is correct
  2. I still have to add the unused update methods inside every class that implements the interface.
1
  • 1
    If you are using Java 8, you can supply empty default implementations for all the methods, so you will not have to redefine all of them in implementors. Btw it would be nice to know what exactly you want to do in these if-instanceof branches. Probable it's better to move this functionality to Class1, Class2? Commented May 12, 2015 at 10:03

4 Answers 4

1
  • There is no problem in overloading a method inside interface
  • An interface is a contract that is mandatory for implementing classes to implement. If you do not want classes to implement that method. There is no need to include it in interface as well.
  • So you can write that overloaded method in those classes only where it is needed. Or Make an abstract class implement the interface and write overloaded method in it.
  • This way you will not end of exposing unnecessary method contract to other classes
Sign up to request clarification or add additional context in comments.

Comments

1

You could try changing the interface to an abstract class and implementing the two methods there. Also, consider using generics.

Comments

1

If you control Class1 and Class2, it would be better to change them like this:

interface Updatable {
    void doUpdate(Observable obj);
}

class Class1 implements Updatable {
    ...
    void doUpdate(Observable obj) { /*class1-specific code*/ }
}

class Class2 implements Updatable {
    ...
    void doUpdate(Observable obj) { /*class2-specific code*/ }
}

This way your main update method will be like this:

public void update (Observable o, Updatable arg0){
    arg0.doUpdate(o);
}

Comments

1

i don't think this is the right way of implementing an Interface. An Interface is a specific Trait (Scala) of a Class. So if it is only one Parameter Type which is changing i would recommend using bounded Type Parameters.

public interface ExtendedObserver<T> extends Observer{
    public void update (Observable obj, T cls);
}

If that doesn't help you, i would recommend you to outsource Class-Specific methods in their own Interface.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.