Java:扩展类并实现具有相同方法的接口。

时间:2022-09-02 12:35:03

Probably the following cannot be done (I am getting a compilation error: "The inherited method A.doSomthing(int) cannot hide the public abstract method in B"):

可能无法完成以下操作(我得到了一个编译错误:“继承方法a . dosomthing (int)无法隐藏B中的公共抽象方法”):

public class A {
    int doSomthing(int x) {
        return x;
    }
}

public interface B {
    int doSomthing(int x);
}

public class C extends A implements B {

    //trying to override doSomthing...

    int doSomthing(int x) {
        return doSomthingElse(x);
    }
}

Assuming I am allowed to change neither A nor B, my question is can I somehow define C in such a way that it will inherit from both A and B (suppose that it is required for some framework that C will be both an instance of A and B).

假设允许我改变A和B,我的问题是,我能否以某种方式定义C,它将从A和B中继承(假设C将是A和B的实例的某个框架需要它)。

Or if not, how would you work around this?

如果没有,你会怎么解决这个问题?

Thanks!

谢谢!

4 个解决方案

#1


24  

make the method public

公开的方法

public class C extends A implements B {

    //trying to override doSomthing...

    public int myMethod(int x) {
        return doSomthingElse(x);
    }
}

interface methods are always public

接口方法总是公开的。

or just use composition instead of inheritance

或者只是使用组合而不是继承。

#2


7  

The method doSomethis() is package-private in class A:

方法doSomethis()在类A中是包-私有的:

public class A {
    int doSomthing(int x) { // this is package-private
        return x;
    }
}

But it is public in the interface B:

但它在界面B中是公开的:

public interface B {
    int doSomthing(int x); // this here is public by default
}

Compiler is taking the doSomething() inherited by C from A which is package-private as the implementation of the one in B which is public. That's why it's complaining -

编译器使用由C继承的doSomething()从一个包-私有的,作为在公共的B中的实现。这就是它抱怨的原因。

"The inherited method A.doSomthing(int) cannot hide the public abstract method in B"

“继承的方法。dosomthing (int)无法隐藏B中的公共抽象方法”

Because, while overriding a method you can not narrow down the access level of the method.

因为,在重写方法时,不能缩小方法的访问级别。

Solution is easy, in class C -

解决办法很简单,在C类。

@Override
public int doSomthing(int x) {
    // ...
}

#3


2  

This has to do with visibility. You are using default (no modifier) visibility in C for myMethod but it needs to be public according to the interface B.

这与能见度有关。您在C中使用默认(没有修改器)可视性,但是需要根据接口B公开。

Now you might think you used the default visibility for all of them, since in neither A, B, nor C did you explicitly select one of public, private, or protected. However, the interface uses public whether or not you explicitly indicate so.

现在您可能认为您使用了所有的默认可见性,因为在A、B和C中都没有显式地选择公共、私有或受保护的。但是,无论您是否显式地指示,该接口都使用公共。

#4


0  

Simply making the method public when overriding it in C will do.

在用C重写方法时,只需将方法公开即可。

#1


24  

make the method public

公开的方法

public class C extends A implements B {

    //trying to override doSomthing...

    public int myMethod(int x) {
        return doSomthingElse(x);
    }
}

interface methods are always public

接口方法总是公开的。

or just use composition instead of inheritance

或者只是使用组合而不是继承。

#2


7  

The method doSomethis() is package-private in class A:

方法doSomethis()在类A中是包-私有的:

public class A {
    int doSomthing(int x) { // this is package-private
        return x;
    }
}

But it is public in the interface B:

但它在界面B中是公开的:

public interface B {
    int doSomthing(int x); // this here is public by default
}

Compiler is taking the doSomething() inherited by C from A which is package-private as the implementation of the one in B which is public. That's why it's complaining -

编译器使用由C继承的doSomething()从一个包-私有的,作为在公共的B中的实现。这就是它抱怨的原因。

"The inherited method A.doSomthing(int) cannot hide the public abstract method in B"

“继承的方法。dosomthing (int)无法隐藏B中的公共抽象方法”

Because, while overriding a method you can not narrow down the access level of the method.

因为,在重写方法时,不能缩小方法的访问级别。

Solution is easy, in class C -

解决办法很简单,在C类。

@Override
public int doSomthing(int x) {
    // ...
}

#3


2  

This has to do with visibility. You are using default (no modifier) visibility in C for myMethod but it needs to be public according to the interface B.

这与能见度有关。您在C中使用默认(没有修改器)可视性,但是需要根据接口B公开。

Now you might think you used the default visibility for all of them, since in neither A, B, nor C did you explicitly select one of public, private, or protected. However, the interface uses public whether or not you explicitly indicate so.

现在您可能认为您使用了所有的默认可见性,因为在A、B和C中都没有显式地选择公共、私有或受保护的。但是,无论您是否显式地指示,该接口都使用公共。

#4


0  

Simply making the method public when overriding it in C will do.

在用C重写方法时,只需将方法公开即可。