java abstract methode call all in subclasses

126 Views Asked by At

I would like to call an abstract method in each subclass. Here is an example:

public abstract class ControllerAbs implements UiListener

/**
 * implements from ui listener. when it's called, then must the ui be updated
 */
@Override
public synchronized void Update() {
    // for change ui elements from another no fx-thread
    // see http://stackoverflow.com/questions/21674152/timer-error-java-lang-illegalstateexception
    Platform.runLater(new Runnable() {
        @Override
        public void run() {
            UiUpdate();
        }
    });
}

/**
 * update ui in subcontroller
 */
protected abstract void UiUpdate();

}

Now, I extend my subclass with the abstract method:

@Override
protected void UiUpdate() {
    // update ui
}

But when I have more than one subclass that will extend from controllerabs, only the first subclass will be updated. What is wrong?

I want a method that will be called in each subclass.

Best regards, sandro

2

There are 2 best solutions below

1
On

Use the keyword super in order to call the method for the supclass defined. E.g. something like the following:

public class SubClass1 extends ControllerAbs {
   @Override
   protected void UiUpdate() {
       // Update for Subclass 1
   }

}

public class SubClass2 extends SubClass1 {
   @Override
   protected void UiUpdate() {
       // Update for Subclass 2
       super.UiUpdate(); // update in the upper subclass
   }

}

Using this keyword, you can refer to the object that is higher in the hierarchy and call its implementation of the method.

0
On

I create a static list of controllers

by create a new controller or subcontroller add it to the list.

    public class ControllerAbs() implements UiListener {
    private static ArrayList<ControllerAbs> controllers;

    // code

    protected registerUiUpdateListener(ControllerAbs controller) {
        controllers.add(controller);
    }

    /**
     * implements from ui listener. when it's called, then must the ui be updated
     */
    @Override
    public synchronized void Update() {
        // for change ui elements from another no fx-thread
        // see http://stackoverflow.com/questions/21674152/timer-error-java-lang-illegalstateexception
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                for (ControllerAbs controller : controllers) {
                controller.uiUpdate(); // update ui in controller
            }
        });
    }

    /**
     * update ui in subcontroller
     */
    protected abstract void uiUpdate();
}

    public class SubClass1 extends ControllerAbs {
        public SubClass1() {
            registerUiUpdateListener(this); // add to list
        }

        @Override
        protected void uiUpdate() {
            // lblTest.setText(testVariable);
        }
    }

    public class SubClass2 extends ControllerAbs {
        public SubClass2() {
            registerUiUpdateListener(this); // add to list
        }

        @Override
        protected void uiUpdate() {
            // lblTest.setText(testVariable);
        }
    }