简体   繁体   中英

Override add() method to private Set<> in another class Java?

I have 2 Java files: File 1 is a given data structure class with methods for a Set and I am not allowed to modify this class at all. File 2 is a class I am making which overrides the add() function in File 1.

File 1

public class MySetClass<E> {
    private final Set<E> set = new HashSet<E>();  //is private

    public boolean add(E element){
        return set.add(element);
    }
 }

File 2

public class OverrideMySetClass<E> extends MySetClass<E> {
    @Override
    public boolean add(E element) {
        System.out.println("Overriden Add");
        return set.add(element);      //ERR: MySetClass<E>.set is not visible (I know it's bc it's private)
    }
}

My question is how do I add items to a set that's not visible to the method that should be overriding add() ?

You should be able to use super.add(element)

In the same way as this refers to the current object, super refers to its direct parent

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