0

Apologies for the super long title. I've had trouble finding a definitive solution to this. I'm pretty new to Java, and my question is in regards to Java's Observer update() method.

[Edit]: I am using Java 8. Using Observable and Observer are requirements of my assignment.

I have these classes in one file, let's say FileA.java:

public abstract class Filter implements Observer
{
    @Override
    public abstract void update(Observable o, Object arg);
}
public abstract class Tester extends Filter
{
    @Override
    public void update(Observable o, Object arg)
    {
        // Some code here. This is activated when another
        // class in FileA.java calls a write() method.
        // The write() method is also located FileA.java.

        Boolean passed = test(message);
    }
    public Boolean test(Message message)
    {
        // Some code here.
    }
}

In another file, let's say FileB.java, I have:

public abstract class SubTester extends Tester
{
    // No update function. Was hoping that the update()
    // method in Tester would call SubTester's test()
    // method.

    public Boolean test(Message message)
    {
        // Some slightly different code here.
    }
}

I'm not including all the code because it's not relevant to my question, in that I know the code in FileA.java is working correctly. My question is in regards to FileB.java.

In FileA.java, this is what happens (correctly):

  1. Another class in FileA.java calls FileA.java's Pipe class's write() method.
  2. write() notifies observers, so it activates Tester's update() method.
  3. Tester's test() method is called and run successfully.

In FileB.java, this is what happens:

  1. Another class in FileB.java calls FileA.java's Pipe class's write() method.
  2. write() notifies observers, so it activates Tester's update() method.
  3. Tester's test() method is called and run.

When I am running FileB.java, I want Tester's update() method to call SubTester's test() method instead of calling Tester's test() method.

I know that a way to fix this would be to add another implementation of the update() method to SubTester. However, I was hoping that since SubTester's test() method, return function, and parameters are the same as its parent class, that there could be a way to prevent this, since the update() method would be the exact same. Therefore, doing this would duplicate the code from the parent class.

Is there a way to get this result without duplicating code with what would be an identical implementation of Tester's update() in SubTester? Thanks in advance!

j.s
  • 1
  • 1
  • 2
    Note: I'm not sure what version of Java you're compiling with, but `Observer` and `Observable` have been deprecated. – Jacob G. Apr 08 '18 at 19:12
  • @Jacob Which classes should we use? – Robert Hume Apr 08 '18 at 19:17
  • @RobertHume https://stackoverflow.com/questions/46380073/observer-is-deprecated-in-java-9-what-should-we-use-instead-of-it – Jacob G. Apr 08 '18 at 19:18
  • Hi @Jacob, thank you for the comment. I am using Java 8 and will add it above. – j.s Apr 08 '18 at 19:24
  • make `test` public in `Tester`, and add `@Override` to `SubTester` method `test` – Nadir Apr 08 '18 at 19:34
  • Class `Tester` and class `SubTester` are both _abstract_, so how do you instantiate them in order to invoke the instance methods `update` and `test`? – Abra Apr 08 '18 at 19:45
  • Hi everyone, thank you so much for your help! @Nadir, I did what you said and it worked. I didn't know I had to add "@Override" to the implementation of the subclass method. Thanks so much for teaching me! – j.s Apr 08 '18 at 21:10

0 Answers0