---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 14 Edition"
java_version: 14
chapter: "Chapter 8. Classes"
section: "8.4.8.3"
heading: "8.4.8.3. Requirements in Overriding and Hiding"
parent: "8.4.8. Inheritance, Overriding, and Hiding"
source: https://docs.oracle.com/javase/specs/jls/se14/html/jls-8.html#jls-8.4.8.3
id: "8.4.8.3"
url: https://jvm.obfuscated.software/jls/se14/8.4.8.3.html
raw: https://jvm.obfuscated.software/jls/se14/8.4.8.3.md
---

# 8.4.8.3. Requirements in Overriding and Hiding

If a method declaration `d_1` with return type R_1 overrides or hides the declaration of another method `d_2` with return type R_2, then `d_1` must be return-type-substitutable ([§8.4.5](8.4.5.md)) for `d_2`, or a compile-time error occurs.

This rule allows for covariant return types - refining the return type of a method when overriding it.

If R_1 is not a subtype of R_2, then a compile-time unchecked warning occurs, unless suppressed by `@SuppressWarnings` ([§9.6.4.5](9.6.4.5.md)).

A method that overrides or hides another method, including methods that implement `abstract` methods defined in interfaces, may not be declared to throw more checked exceptions than the overridden or hidden method.

In this respect, overriding of methods differs from hiding of fields ([§8.3](8.3.md)), for it is permissible for a field to hide a field of another type.

More precisely, suppose that B is a class or interface, and A is a superclass or superinterface of B, and a method declaration `m_2` in B overrides or hides a method declaration `m_1` in A. Then:

- If `m_2` has a `throws` clause that mentions any checked exception types, then `m_1` must have a `throws` clause, or a compile-time error occurs.
- For every checked exception type listed in the `throws` clause of `m_2`, that same exception class or one of its supertypes must occur in the erasure ([§4.6](4.6.md)) of the `throws` clause of `m_1`; otherwise, a compile-time error occurs.
- If the unerased `throws` clause of `m_1` does not contain a supertype of each exception type in the `throws` clause of `m_2` (adapted, if necessary, to the type parameters of `m_1`), then a compile-time unchecked warning occurs, unless suppressed by `@SuppressWarnings` ([§9.6.4.5](9.6.4.5.md)).

It is a compile-time error if a type declaration T has a member method `m_1` and there exists a method `m_2` declared in T or a supertype of T such that all of the following are true:

- `m_1` and `m_2` have the same name.
- `m_2` is accessible ([§6.6](6.6.md)) from T.
- The signature of `m_1` is not a subsignature ([§8.4.2](8.4.2.md)) of the signature of `m_2`.
- The signature of `m_1` or some method `m_1` overrides (directly or indirectly) has the same erasure as the signature of `m_2` or some method `m_2` overrides (directly or indirectly).

These restrictions are necessary because generics are implemented via erasure. The rule above implies that methods declared in the same class with the same name must have different erasures. It also implies that a type declaration cannot implement or extend two distinct invocations of the same generic interface.

The access modifier of an overriding or hiding method must provide at least as much access as the overridden or hidden method, as follows:

- If the overridden or hidden method is `public`, then the overriding or hiding method must be `public`; otherwise, a compile-time error occurs.
- If the overridden or hidden method is `protected`, then the overriding or hiding method must be `protected` or `public`; otherwise, a compile-time error occurs.
- If the overridden or hidden method has package access, then the overriding or hiding method must *not* be `private`; otherwise, a compile-time error occurs.

Note that a `private` method cannot be overridden or hidden in the technical sense of those terms. This means that a subclass can declare a method with the same signature as a `private` method in one of its superclasses, and there is no requirement that the return type or `throws` clause of such a method bear any relationship to those of the `private` method in the superclass.

**Example 8.4.8.3-1. Covariant Return Types**

The following declarations are legal in the Java programming language from Java SE 5.0 onwards:

```
class C implements Cloneable {
    C copy() throws CloneNotSupportedException {
        return (C)clone();
    }
}
class D extends C implements Cloneable {
    D copy() throws CloneNotSupportedException {
        return (D)clone();
    }
}
```

The relaxed rule for overriding also allows one to relax the conditions on abstract classes implementing interfaces.

**Example 8.4.8.3-2. Unchecked Warning from Return Type**

Consider:

```
class StringSorter {
    // turns a collection of strings into a sorted list
    List toList(Collection c) {...}
}
```

and assume that someone subclasses `StringSorter`:

```
class Overrider extends StringSorter {
    List toList(Collection c) {...}
}
```

Now, at some point the author of `StringSorter` decides to generify the code:

```
class StringSorter {
    // turns a collection of strings into a sorted list
    List<String> toList(Collection<String> c) {...}
}
```

An unchecked warning would be given when compiling `Overrider` against the new definition of `StringSorter` because the return type of `Overrider.toList` is `List`, which is not a subtype of the return type of the overridden method, `List<String>`.

**Example 8.4.8.3-3. Incorrect Overriding because of `throws`**

This program uses the usual and conventional form for declaring a new exception type, in its declaration of the class `BadPointException`:

```
class BadPointException extends Exception {
    BadPointException() { super(); }
    BadPointException(String s) { super(s); }
}
class Point {
    int x, y;
    void move(int dx, int dy) { x += dx; y += dy; }
}
class CheckedPoint extends Point {
    void move(int dx, int dy) throws BadPointException {
        if ((x + dx) < 0 || (y + dy) < 0)
            throw new BadPointException();
        x += dx; y += dy;
    }
}
```

The program results in a compile-time error, because the override of method `move` in class `CheckedPoint` declares that it will throw a checked exception that the `move` in class `Point` has not declared. If this were not considered an error, an invoker of the method `move` on a reference of type `Point` could find the contract between it and `Point` broken if this exception were thrown.

Removing the `throws` clause does not help:

```
class CheckedPoint extends Point {
    void move(int dx, int dy) {
        if ((x + dx) < 0 || (y + dy) < 0)
            throw new BadPointException();
        x += dx; y += dy;
    }
}
```

A different compile-time error now occurs, because the body of the method `move` cannot throw a checked exception, namely `BadPointException`, that does not appear in the `throws` clause for `move`.

**Example 8.4.8.3-4. Erasure Affects Overriding**

A class cannot have two member methods with the same name and type erasure:

```
class C<T> {
    T id (T x) {...}
}
class D extends C<String> {
    Object id(Object x) {...}
}
```

This is illegal since `D.id(Object)` is a member of `D`, `C<String>.id(String)` is declared in a supertype of `D`, and:

- The two methods have the same name, `id`
- `C<String>.id(String)` is accessible to `D`
- The signature of `D.id(Object)` is not a subsignature of that of `C<String>.id(String)`
- The two methods have the same erasure

Two different methods of a class may not override methods with the same erasure:

```
class C<T> {
    T id(T x) {...}
}
interface I<T> {
    T id(T x);
}
class D extends C<String> implements I<Integer> {
   public String  id(String x)  {...}
   public Integer id(Integer x) {...}
}
```

This is also illegal, since `D.id(String)` is a member of `D`, `D.id(Integer)` is declared in `D`, and:

- The two methods have the same name, `id`
- `D.id(Integer)` is accessible to `D`
- The two methods have different signatures (and neither is a subsignature of the other)
- `D.id(String)` overrides `C<String>.id(String)` and `D.id(Integer)` overrides `I.id(Integer)` yet the two overridden methods have the same erasure
