Composition in effective takes precedence over inheritance

public class InstrumentedHashSet<E> extends HashSet {

  private long count;

  @Override
  public boolean add(Object o) {
    countPP;
    return super.add(o);
  }

  @Override
  public boolean addAll(Collection collection) {
    count += collection.size();
    return super.addAll(collection);
  }

  public long getCount() {
    return count;
  }

  public static void main(String[] args) {
    HashSet hashSet = new HashSet(3);
    hashSet.add(1);
    hashSet.add(2);
    hashSet.add(3);

    InstrumentedHashSet<Integer> instrumentedHashSet = new InstrumentedHashSet<>();
    instrumentedHashSet.addAll(hashSet);
    //63
    System.out.println(instrumentedHashSet.getCount());
  }
}

---------------------

this first calls the addAll, of the object of instrumentedHashSet, and then count becomes 3, and then calls super.addAll, that is, the addAll in hashset"s addAll,hashset should call hashset"s add, right? Then why is count 6? shouldn"t it be 3?

I debugged the program and found that it was the addAll in hashset that called InstrumentedHashSet"s add,. Why? Is it because the add method is overridden? The parent class adjusts its own method, only to find that the quilt class is overwritten, so the method of the subclass is adjusted.

The

subclass calls method A, and if it is not overridden, the parent class"s method An is called, and if overridden, its own method An is called.
does the parent class call its own method A to see if it is overridden by the subclass? A little confused.

The

question asks: what kind of call chain is this? (write down all your thoughts, it may be a little messy, don"t spray for beginners.)

Aug.03,2021

I'm not familiar with java, but I get the same result from js's point of view. Your problem can be reduced to the following minimum model

class Parent{
    add(){
        console.log("parent")
    }
    addAll(){
        debugger;
        this.add()
    }
}
class Child extends Parent{
    constructor(){
        super()
    }
    add(){
        console.log("child")
    }
    addAll(){
        return super.addAll()
    }
}
var child = new Child();
child.addAll();

clipboard.png

childparent
java

clipboard.png

clipboard.png


in the case of debug, you can see that this is still an instance of the subclass when you enter the parent class, so calling add will still call the add method of the subclass.

the reason is that the parent class constructor is called when the child class object is created, but a parent class instance is not built, so no parent class instance calls its own method. Super is like a tag that tells jvm to call the method of the parent class here.


the addAll actually called is the addAll,hashSet in AbstractCollection and the add in addAll,AbstractCollectoin needs to be rewritten. Your calling class is InstrumentedHashSet,. He will first look for the rewriting of add from this class, so he called his add.
if your InstrumentedHashSet does not override add, he will call hashSet's add implementation

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b408ee-2b887.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b408ee-2b887.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?