java - Two interface have same method name with different return type -


this question understanding interfaces in java. here simple example of implementing interfaces in java.

interface parenta {     void display(); }  interface parentb {     int display(); }  class child implements parenta, parentb {     @override     public void display() {         system.err.println("child parenta");     }     //error : return type incompatible parentb.display()      //so added method int return type     @override     public int display() {         system.err.println("child parentb");     } } 

this case can happen in large java application 2 interface can have method same name. thought since return type different jvm know interface's method overriding.

what best explanation this? situation make sense?

thanks in advance

because method same signature not allowed, confuses compiler detect exact override-equivalent method declared once.

jls (§8.4.2)

2 methods or constructors, m , n, have same signature if have,

  • the same name
  • the same type parameters (if any) (§8.4.4), and
  • after adapting formal parameter types of n the type parameters of m, same formal parameter types.

it compile-time error declare 2 methods override-equivalent signatures in class.


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -