“不能降低继承方法的可见性"的含义带接口 [英] Meaning of "Cannot reduce the visibility of the inherited method" with an interface

查看:44
本文介绍了“不能降低继承方法的可见性"的含义带接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文件:

public interface PrintService {
    void print(PrintDetails details);
    class PrintDetails {
        private String printTemplate;
    }
    public interface Task {
        String ACTION = "print";
    }
}

public class A implements PrintService {
    void print(PrintDetails details) {
        System.out.println("printing: " + details);
    }
    String action = PrintService.Task.ACTION;   
}

我认为代码看起来不错,但我在第二个文件中遇到了 void print(PrintDetails details) { 行的错误,指出:

I thought the code looks okay, but I am getting an error in the second file for the line void print(PrintDetails details) { that states:

无法降低从 PrintService 继承的方法的可见性.

Cannot reduce the visibility of the inherited method from PrintService.

谁能解释一下这对我意味着什么?

Can someone explain what this means for me?

推荐答案

在 Java 接口中,每个方法默认都是 public:

In a Java interface each method is by default public:

接口主体中的每个方法声明都是隐式抽象的,所以它的主体总是用分号表示,而不是块.

Every method declaration in the body of an interface is implicitly abstract, so its body is always represented by a semicolon, not a block.

接口主体中的每个方法声明都是隐式公开的.[..]

Every method declaration in the body of an interface is implicitly public. [..]

在实现类中,不允许降低可见性,也不允许不指定访问修饰符:

In an implementing class you are not allowed to reduce the visibility, and by not specifying an access modifier:

void print(){..}

您正在指定访问级别 default,其可见性低于 public.

you are specifying the access level default, which has lower visibility than public.

这篇关于“不能降低继承方法的可见性"的含义带接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆