When the method signature name and parameters are the same in the superclass and the child class?

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?

When the method signature name and parameters are the same in the superclass and the child class?

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 : http://codeinventions.blogspot.in/2014/11/covariant-contravariant-and-class-invariant-example-and-difference-in-java.html

answered Dec 31, 2015 at 6:12

When the method signature name and parameters are the same in the superclass and the child class?

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
}

https://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:

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.

    class ShapeFactory {
        public Shape newShape() {}
   }

   class CircleFactory extends ShapeFactory {
        @Override
        public Circle newShape() {}
   } 

answered Dec 31, 2015 at 6:13

bNdbNd

7,4126 gold badges37 silver badges71 bronze badges

2

When a method has the same name and takes the same parameters as a method in the parent class?

Overriding means having two methods with the same method name and parameters (i.e., method signature). One of the methods is in the parent class and the other is in the child class.

When a child class declares a method with the same signature as a method in the parent class this process is called?

In Java, method overriding occurs when a subclass (child class) has the same method as the parent class. In other words, method overriding occurs when a subclass provides a particular implementation of a method declared by one of its parent classes.

What is it called when a method in a subclass has the same signature as a method in the superclass?

Instance Methods An instance method in a subclass with the same signature (name, plus the number and the type of its parameters) and return type as an instance method in the superclass overrides the superclass's method.

When a method has same signature in the base class?

If a method in a derived class has the same method signature as its base class's method, it's referred to as an overridden method. Overridden methods are discussed along with polymorphism in chapter 6.