简体   繁体   中英

One interface with two methods having same name but different signature

I have one Interface, build and provided as jar, java src, and target = 1.5

interface IGetter {
   int getInternalID(final long externId);
   int getInternalID(long externId, char specifier);
}

And I implement an inner static class in java 1.8 implementing this interface

public static Calculator {
...
    public static class LookupTable implements IGetter {
                /* (non-Javadoc)
                 * @see com.mycompany.IGetter#getInternalID(long)
                 */
                @Override
                public int getInternalID(long externId) {
                   ....
                   return internId;
                }

                /* (non-Javadoc)
                 * @see com.mycompany.IGetter#getInternalID(long, char)
                 */
                @Override   <-- Here compiler error; This method is not detected
                as beeing in the interface.
                public int getInternalID(long externalId, char specifier) {
                    return ...;
                }
    }
}

The second getInternal(long, char) method is not detected by the compiler as beeing an interface method.

Clicking on the jar file that contains the interface, even the class viewer in eclipse, show on the left pane in the method overview that the method is missing, while in the editor view it shows that the method is implemented in the interface.

What is wrong, Is it not allowed to have the same name for two methods in an interface?
Why the java compiler compiles the interface? (src and target = java 1.5)
Why does the java 1.8 compile does not recognize it as interface method. (src and target 1.8)

Compiler error is: The method getInternalID(long, char) of type Calculator.LookupTable must override or implement a supertype method Calculator.java

The cause was that there was another jar file in the classpath which was compiled with an old version of the IGetter interface.

Thanks to user purring pigeon, for his question whether I am sure that I import the correct interface.

Because you are using Java 5 you cannot use the @Override annotation. It will not work with any version less than Java 6. This why the annotation is optional in the older versions since if it was mandatory it would break backwards compatibility.

In Java 5: the @Override annotation refers only to superclasses.

In Java 6+: the @Override annotation refers to superclasses and implemented methods.

Your code is giving an error because in Java 5 this is not a valid annotation for the methods you are implementing in your class. Remove it and recompile.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM