Why does Java's Area#equals method not override Object#equals?

后端 未结 2 1653
陌清茗
陌清茗 2021-02-07 01:19

I just ran into a problem caused by Java\'s java.awt.geom.Area#equals(Area) method. The problem can be simplified to the following unit test:

@org.j         


        
2条回答
  •  悲&欢浪女
    2021-02-07 01:52

    "Why does Java's Area#equals method not override Object#equals?"

    Because overriding is not necessary for overloaded methods where the parameters are of differing types.

    An overridden method would have the exact same method name, return type, number of parameters, and types of parameters as the method in the parent class, and the only difference would be the definition of the method.

    This case does not compel us to override but it is overloading as it follows these rules:

    1.) The number of parameters is different for the methods.

    2.) The parameter types are different (like changing a parameter that was a float to an int).

    "why did they not just name the questionable method in a way that does not clash with such a fundamental concept as the equals method?"

    Because this could trip people up going into the future. If we had a time machine to the 90's we could do it without this concern.

提交回复
热议问题