Consider the scenario where we have two classes related by inheritance

class Parent {    
}

class Child extends Parent{   
}
  1. If Parent declares a method with folowing signature:

    public String method(Number n) / /parent 1
    

    I know that in Child I can declare method with following signature(should be override)

    public String method(Number n) // chilld 1_1
    
  2. But if Parent contains following signature:

    public <T extends Number> String method(T t) //parent2
    

    Child can override it by following signatures(2, separated):

    public String method(Number n)//chilld 2_1
    

    or

     public<T extends Number> String method(T t)// chilld 2_2
    

Are there other variants for overriding parent1 or parent2 ?

有帮助吗?

解决方案

If you are only looking for a variant then how the following -

public String method(Number n) throws RuntimeException

or,

public <T extends Number> String method(T n) throws RuntimeException

Also, the other that don't apply in case of String, because it's final, is that the overriding methods are allowed return a subtype of return type in the parent.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top