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):
- Another class in FileA.java calls FileA.java's Pipe class's write() method.
- write() notifies observers, so it activates Tester's update() method.
- Tester's test() method is called and run successfully.
In FileB.java, this is what happens:
- Another class in FileB.java calls FileA.java's Pipe class's write() method.
- write() notifies observers, so it activates Tester's update() method.
- 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!