2

I have a followup question to the one asked here:

Java Abstract Class (this keyword)

Using the following code:

public class SuperClass() {
    boolean isDoneLoading = false;
    ArrayList items;
   public SuperClass() {
        loadData();
        items = new ArrayList();
    }
 
    protected void loadData() {
        // loadData is a class that requests data from remote server, populates "items"
        // and then calls doneLoading when done;

    }
    protected void doneLoading() {
        isDoneLoading = true;
    }
}

public class ExtendedClass extends SuperClass {

   public ExtendedClass() {
        super();    
   }
    protected void doneLoading() {
        // how can I get THIS method to be called when the super's doneLoading is called?
    }
}

// some other class
new ExtendedClass();

How can I get the "child's" (ExtendedClass) doneLoading() class when the super's doneLoading class is called?

Community
  • 1
  • 1
StackOverflowed
  • 5,854
  • 9
  • 55
  • 119
  • 1
    call `super` in your childs `doneLoading()` – Aman J Sep 15 '12 at 22:47
  • Not directly related to your question, but take a look at http://stackoverflow.com/a/3404369 about possible problems with using overridable method calls in constructors. `loadData()` should be `private` when used in the constructor. – Modus Tollens Sep 19 '12 at 07:47
  • Just a comment on code convention: `SuperClass` and `ExtendedClass` are really not very good names for classes, the reason being that the class name is also the name for the type of objects it defines. Clearly, an instance of your `SuperClass` class is _not_ actually a superclass of anything. – AJMansfield Apr 29 '13 at 21:09

4 Answers4

3

If you want the parent doneLoading followed by the childs version of the routine, and yet retain this functionality in the parent, they must be named differently. E.g.,

public abstract class SuperClass {
    public void doneLoading() {
       isDoneLoading();
    }
    abstract isDoneLoading();
}

The other option delegates the responsibility to the child to invoke super.doneLoading().

Johan Sjöberg
  • 47,929
  • 21
  • 130
  • 148
2

If you write:

ExtendedClass ec = new ExtendedClass();

loadData will be called in the super class (there is no such method in ExtendedClass), but doneLoading will be called in ExtendedClass. So the super class doneLoading method won't be called because you have overriden it.

ps: this comment:

loadData is a class that requests data from remote server

makes me think that you are doing too much stuff in your constructor.

assylias
  • 321,522
  • 82
  • 660
  • 783
1

Ignoring the question of whether what you're doing is good design, just do this:

class ExtendedClass {
    protected void doneLoading() {
        super.doneLoading();
        // what needs to be called after SuperClass.doneLoading();
    }
}

Another alternative would be using the Observer pattern. (Or maybe Chain of Responsibility.) Basically have the child classes that add a processing step register listeners for a "done loading" event emitted by the superclass.

millimoose
  • 39,073
  • 9
  • 82
  • 134
0

Doneloading function which exists in SuperClass have been overridden. You are trying to call the overridden function . It seems nonsense to override if you call the function of the super class.

oiyio
  • 5,219
  • 4
  • 42
  • 54