Problem overriding ArrayList add method

前端 未结 7 1942
一整个雨季
一整个雨季 2021-01-27 19:45

I have a class that is extending Java\'s ArrayList. I\'m currently using Java build 1.6.0_22-b04. Looks like this:

public class TokenSequence extends ArrayList&         


        
7条回答
  •  死守一世寂寞
    2021-01-27 20:30

    Change it to public boolean add(Token o). (Note return and parameter type)

    In order to override a method, your override must have the exact same signature, including the return type.
    Since your method has a different return type, it doesn't actually override the base add method.

    The reason that it won't even compile is that because it doesn't override the base method, you end up with two different add methods, both of which are callable by your derived class.
    However, due to type erasure, they both actually take an Object parameter, which is illegal.

提交回复
热议问题