shedhorn
shedhorn

Reputation: 3

Strange Polymorphism Need

I have a class with an eventOccurred that does its work inside SwingUtilities.invokeLater (in order to be on the EDT). This class will be extended. I want to force anything extending this class to also put their eventOcurred work inside invokeLater and can't see how to do it. Putting final on eventOccurred in my base class won't work because I need to allow eventOccurred to be overriden.

Upvotes: 0

Views: 34

Answers (1)

hankd
hankd

Reputation: 649

Can you implement a method like "doStuff()" in the base class? Then your eventOccurred method invokes this method by using the invokeLater. Then you override doStuff(), and calling eventOccurred forces it to be done on the EDT.

Like so:

class BaseClass {
    public void doStuff() {
        System.out.println("Base class do stuff");
    }

    public void EventOccurred() {
        //Put this on the EDT
        doStuff();
    }
};

class SubClass extends BaseClass {
    @Override
    public void doStuff() {
        System.out.println("Subclass do stuff");
    }
}

Then running:

BaseClass base = new BaseClass();
SubClass test = new SubClass();

base.EventOccurred();
test.EventOccurred();

Yields:

Base class do stuff
Subclass do stuff

Upvotes: 1

Related Questions