简体   繁体   中英

Override doesn't seem to work

I am a bit confused, I thought this should work. Its simply a parent and a childclass, I cannot figure out why a) eclipse complains, and b) the overridden method does not get called in an instantiated object.

public class Selector {

private Node rootNode;
private Grid childGrid;

public Selector(){
    super();
}

public Selector(Grid childGrid){
    this();
    this.childGrid = childGrid;
}

public Selector(Node rootNode,Grid childGrid){
    this();
    this.rootNode = rootNode;
    this.childGrid = childGrid;
}

private ArrayList<ArrayList<String>> filter(ArrayList<String> keys){
    return null;
}

private ArrayList<ArrayList<String>> innerEneryOrder(ArrayList<ArrayList<String>> children){
    return children;
}

private ArrayList<ArrayList<String>> outerEneryOrder(ArrayList<ArrayList<String>> children){
    return children;
}}

Ok, here is the derived class:

public class StandardSelector extends Selector {

    @Override
private ArrayList<ArrayList<String>> filter(ArrayList<String> keys){
    ArrayList<ArrayList<String>> ret = new ArrayList<>();
    for (String s: keys){
        ArrayList<String> aL = new ArrayList<String>();
        aL.add(s);
        ret.add(aL);
    }
    return ret;
}}

So, where is the problem?

Increase the visibility of the filter method so it can be overridden.

From JLS 6.6-5

A private class member or constructor is accessible only within the body of the top level class (§7.6) that encloses the declaration of the member or constructor.

Therefore, replace

private ArrayList<ArrayList<String>> filter(ArrayList<String> keys){

with

protected List<List<String>> filter(List<String> keys) {

Change the visibility of the filter() method to protected in Super class so it can be overridden. Private methods cannot be override. In your case you have just created a new method for subclass it has nothing to do with base class filter() method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM