vincendep
vincendep

Reputation: 607

Is it possible to chain overrided method call from top to bottom?

Suppose I have this hierarchy of classes

class Parent {
    protected List<Object> things = new List<Object>;

    public Parent() {
        things.addAll(declareThings());
    }

    public List<Object> declareThings() {
        // things declaration
    }
}

class Child extends Parent {

    public List<Object> declareThings() {
        // additional things declaration
    }
}

class GrandChild extends Child {
    
    public List<Object> declarThings() {
        // addtional things
    }
}

I would like that creating an instance of say GrandChild, I get all the declared things in the hierarchy. Is it possible to implement such behaviour?

Upvotes: 0

Views: 36

Answers (1)

Zelig
Zelig

Reputation: 1808

For each subclass, add a constructor where the first instruction is super();.

Upvotes: 2

Related Questions