简体   繁体   中英

Same method with different return types in abstract class and interface

Just extending the question..

Same method in abstract class and interface

Suppose a class implements an interface and extends an abstract class and both have the same method (name+signature), but different return types. Now when i override the method it compiles only when i make the return type same as that of the interface declaration.

Also, what would happen if the method is declared as private or final in the abstract class or the interface?

**On a side note. Mr. Einstein stuck to this question for an abominable amount of time during an interview. Is there a popular scenario where we do this or he was just showing off?

If the method in abstract class is abstract too, you will have to provide its implementation in the first concrete class it extends. Additionally, you will have to provide implementation of interface. If both the methods differ only in return type, the concrete class will have overloaded methods which differ only in return type. And we can't have overloaded methods which differ only in return type, hence the error.

interface io {
    public void show();
}

abstract class Demo {
    abstract int show();         
}

class Test extends Demo implements io {  
    void show () {     //Overloaded method based on return type, Error
    }

    int show() {       //Error
        return 1;
    }

    public static void main (String args[]) {

    }
}

No, same method names and parameters, but different return types is not possible in Java. The underlying Java type system is not able* to determine differences between calls to the methods at runtime.

(*I am sure someone will prove me wrong, but most likely the solution is considered bad style anyways.)

Regarding private/final: Since you have to implement those methods, neither the interface method nor the abstract method can be final. Interface methods are public by default. The abstract method can't be private, since it must be visible in the implementing class, otherwise you can never fulfill the method implementation, because your implementing class can't "see" the method.

With Interfaces the methods are abstract and public by default , so they cant have any other access specifier and they cant be final

With abstract class , abstract methods can have any access specifier other than private and because they are abstract they cant be final

While overriding , the method signature has to be same ; and covariant(subclass of the declared return type) return types are allowed

A class cannot implement two interfaces that have methods with same name but different return type. It will give compile time error. Methods inside interface are by default public abstract they don't have any other specifier.

interface A
{
  public void a();
}
interface B
{
  public int a();
}

class C implements A,B
{

  public void a() // error
 {
    //implementation
 }

 public int a() // error
 {
    //implementation
 }

 public static void main(String args[])
 {

 }
}

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