java - Are methods overridden when methods have the same name but different signatures? -
public class superclass { void method(object a){ } } public class subclass extends superclass { void method(string a){ } }
in class above, method in superclass has parameter of type object while subclass has parameter of type string. string subclass of object. question is, method overriden in case?
the famous example of confusion caused issue raise involves equals
:
public class banana { private final double weight; public boolean equals(banana that) { return this.weight == that.weight; } }
many people think valid override of equals
, it's method unrelated object#equals(object)
, not involved in equals comparison.
this 1 of known pitfalls of java's type system take care right.
to confuse matters further, are allowed specialize return type (the return type covariant):
public abstract class fruittree { public abstract object getfruit(); } public class bananatree { ... @override public banana getfruit() { return this.bananas.iterator().next(); } }
note notion of method signature not include return type. method signature key concept in mechanism of static (compile-time) method resolution, , each signature separate point of dynamic method dispatch.
Comments
Post a Comment