I'm new to Java and I'm trying to learn the concept of "Overriding method" as part of inheritance.
If an instance method in the subclass has the same signature(i.e. name plus the number and the type of its parameters) as the method in the superclass but it has a DIFFERENT return type, does the instance method still override the method in the superclass? Or is it a completely new method?
halfer
19.6k17 gold badges92 silver badges176 bronze badges
asked Dec 31, 2015 at 6:08
No when you have DIFFERENT return type, that is completely a new method. Not overridden.
Return type is also part of method signature. You need to obey all the rules to override.
And interesting part to note here is Covariance
Consider you have Parent and Child relationship and trying to override the methods of Parent in Child, Co-variance means that the overriding method returning a more specific type. Below example shows you the same, Parent method returning Object and where as the Child method decided to return a specific type (String) where String is child of Object class. Hence covariance existed here.
Covariant return types :
public class Parent{ public Object doSomething(){} } public class Child extends Parent{ public String doSomething() {} }If you are interested give a read on my blog post : //codeinventions.blogspot.in/2014/11/covariant-contravariant-and-class-invariant-example-and-difference-in-java.html
answered Dec 31, 2015 at 6:12
Suresh AttaSuresh Atta
119k37 gold badges191 silver badges301 bronze badges
3
it's an override only if the overriding method has a return type that is a subtype of the type returned by overridden method.
answered Dec 31, 2015 at 6:11
RamanlfcRamanlfc
8,1781 gold badge17 silver badges24 bronze badges
2
For a method with a different return type from a same-named parent method with the same argument arity and types, if the return type is a subtype of the parent method's return type, then it's a legal override. If not, it's a compiler error.
public interface Exemplary { CharSequence getText(); } public class Example implements Exemplary { @Override public String getText(); // legal } public class BadExample implements Exemplary { @Override public Integer getText(); // error }//docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.4.8.1
answered Dec 31, 2015 at 6:20
Lew BlochLew Bloch
3,3281 gold badge15 silver badges10 bronze badges
overriding method has the same name, number and type of parameters, and return type as the method that it overrides. An overriding method can also return a subtype of the type returned by the overridden method. This subtype is called a covariant return type. Let us say example:
class ShapeFactory { public Shape newShape() {} } class CircleFactory extends ShapeFactory { @Override public Circle newShape() {} }The subclass method's return type R2 may be different from superclass method's return type R1, but R2 should be a subtype of R1. i.e., subclass can return type may be a subtype of superclass return type.
answered Dec 31, 2015 at 6:13
bNdbNd
7,4126 gold badges37 silver badges71 bronze badges
2