Java Interface: Inheritance, Overriding, and Overloading Methods

In "Java â„ĸ Programming Language, Fourth Edition" by Ken Arnold, James Gosling, David Holmes, he noted that:

: (4.3.2) Similarly, if an interface inherits more than one method with the same signature, or if the class implements different interfaces containing a method with the same signature, there is only one such method. The implementation of this method is ultimately determined by the implementation of the interfaces class, and there is no ambiguity. If the methods have the same signature but different types of returned data, then one of the types of returned data must be a subtype of all the others, otherwise a time error occurs compilation. method that returns this common subtype. "

Can someone give me an example of a code that justifies the points above a paragraph?

I tried to write the code and check what is mentioned, but I get a compile-time error the sub-interface hides the base interface method, so it can only implement the sub-interface method.

Thanks in advance. -Arun

+7
java override overloading interface
source share
5 answers

In the following two interfaces, methodA() identically defined in terms of parameters (none) and return type (int). The implementation class below defines one method with this exact signature. Since this corresponds to both interfaces, you have no problem - any calls made through a link such as InterfaceA or InterfaceB will be sent to this implementation.

The second methodB() is defined as returning any subtype of Number (or Number itself) in InterfaceA . InterfaceB defines methodB() as returning an Integer , which is a subtype of Number . The implementation class actually implements the method with Integer , which corresponds to the contract of both InterfaceA and InterfaceB . There are no problems. The noticed case of methodB() , implemented as returning a Double , however, will not work: although it will satisfy the InterfaceA contract, it will conflict with InterfaceB (which requires Integer ).

If InterfaceA and InterfaceB also indicated (different) contracts for methodC() (commented out in the example), this would be inconsistent and would cause a compiler error. Implementation of both signatures (differing only in return type) is not allowed in Java.

The above rules will also be the case if any parameters were added to the methods. For simplicity, I saved this from an example.

 public interface InterfaceA { public int methodA(); public Number methodB(); // public int methodC(); // conflicting return type } public interface InterfaceB { public int methodA(); public Integer methodB(); // public String methodC(); // conflicting return type } public class ImplementationOfAandB implements InterfaceA, InterfaceB { public int methodA() { return 0; } public Integer methodB() { return null; } // This would NOT work: // public Double methodB() { // return null; // } } 
+2
source share
 interface A { void method(); Object returnMethod(); } interface B { void method(); B returnMethod(); } class Impl implements A,B { void method() { } B returnMethod() { } } 

As you can see, Impl.method() implements both A.method() and B.method() , and Impl.returnMethod() returns B , which is a child of Object , thereby executing A.returnMethod() contract . Will the latter require a return type that is not the parent of the return type B.returnMethod() , which would be a compilation error, since such an implementation could not exist in Impl .

+8
source share
 interface A { void foo(); //int bar(); <-- conflicts with B.bar() because of different return type } interface B { void foo(); //double bar(); <-- conflicts with A.bar() because of different return type } class C implements A, B { void foo() // this implements A.foo() AND B.foo() { ... } } 
+1
source share

Is that what you mean?

 interface A { Object get(); } interface B { Number get(); } abstract class MyClass implements A, B { // Try to override A.get, but cause a compile error. public Object get() { return null; } } 

Such a method in MyClass is automatically generated by javac as a synthetic bridge method. You must implement a single method that returns a type compatible with all implemented / overridden methods (in this case, Number / Integer / Double / etc).

+1
source share
 /** * This is what you have */ interface IXR { //bla-bla-bla } class CXR implements IXR { //concrete implementation of bla-bla-bla } interface IX { public IXR f(); } interface IYR { //some other bla-bla-bla } class CYR implements IYR { //concrete implementation of the some other bla-bla-bla } interface IY { public IYR f(); } /** * This is what you need to add */ interface IZR extends IXR, IYR { //EMPTY INTERFACE } class CZXR extends CXR implements IZR { //EMPTY CLASS } class CZYR extends CYR implements IZR { //EMPTY CLASS } class CZ implements IX, IY { public static boolean someCondition = true; public IXR implementationOf_X_f() { System.out.println("CXR"); return new CZXR(); } public IYR implementationOf_Y_f() { System.out.println("CYR"); return new CZYR(); } public IZR f() { if (someCondition) { return (IZR) implementationOf_X_f(); } else { return (IZR) implementationOf_Y_f(); } } } /** * This is the usage of the required class */ class program { public static void main(String[] x) { CZ o = new CZ(); IZR r = of(); if (CZ.someCondition) { CXR xr = (CXR) r; //bla-bla-bla } else { CYR yr = (CYR) r; //bla-bla-bla } } } /** * This is what you have */ interface IXR { //bla-bla-bla } class CXR implements IXR { //concrete implementation of bla-bla-bla } interface IX { public IXR f(); } interface IYR { //some other bla-bla-bla } class CYR implements IYR { //concrete implementation of the some other bla-bla-bla } interface IY { public IYR f(); } /** * This is what you need to add */ interface IZR extends IXR, IYR { //EMPTY INTERFACE } class CZXR extends CXR implements IZR { //EMPTY CLASS } class CZYR extends CYR implements IZR { //EMPTY CLASS } class CZ implements IX, IY { public static boolean someCondition = true; public IXR implementationOf_X_f() { System.out.println("CXR"); return new CZXR(); } public IYR implementationOf_Y_f() { System.out.println("CYR"); return new CZYR(); } public IZR f() { if (someCondition) { return (IZR) implementationOf_X_f(); } else { return (IZR) implementationOf_Y_f(); } } } /** * This is the usage of the required class */ class program { public static void main(String[] x) { CZ o = new CZ(); IZR r = of(); if (CZ.someCondition) { CXR xr = (CXR) r; //bla-bla-bla } else { CYR yr = (CYR) r; //bla-bla-bla } } } 
+1
source share

All Articles