简体   繁体   English

Java-带接口的受保护的修饰符

[英]Java - Protected Modifier with Interface

public interface ICalculator {

    public double multi(double a, double b);

    public int add(int a, int b);


    public int sub(int a, int b);
}

public class Calculator extends ICalculator {

    protected int add(double a, double b) {
        return a+b;
    }

    public double sub(int zahl1, int zahl2 ) {
        return a*b;
    }
}

why i can't use in the class Calculator a protected method ? 为什么我不能在计算器类中使用受保护的方法? My answer is that "protected" is it useful in the same class and in the subclasses . 我的回答是,“ protected”在同一个类和子类中有用吗? Well couldn't i also think that a method in an implemented class from Interface is also inherited, like subclasses. 我也不能认为接口的已实现类中的方法也像子类一样被继承了。

You cannot add more restriction to the method you override in the sub class.In the same way, you cannot add more restriction to the method you implement from an interface. 您不能为在子类中重写的方法添加更多限制。以同样的方式,您不能为从接口实现的方法添加更多限制。

Now, since methods defined in an interface is by default - public (Yes, you don't need to write it explicitly), so you cannot make your method protected in implementing class. 现在,由于默认情况下接口中定义的方法是public (是的,不需要显式地编写它),因此在实现类时不能使您的方法protected

The reason is straight, when you are working with polymorphism, you can instantiate your implementing class and store its reference in the interface type . 原因很简单,当您使用多态时,您可以实例化implementing class并将其引用存储在interface type

ICalculator calc = new Calculator();  //valid

calc.add(1, 2);   // Compiler sees ICalculator method.

Now, when you invoke the add method using ICalculator reference, compiler only sees the method defined in ICalculator , and approves access accordingly. 现在,当您使用ICalculator引用调用add方法时,编译器仅看到ICalculator定义的方法,并相应地批准访问。 But at runtime, the actual method invoked is form Calculator class, and what happens now, if that method is actually invoked from a different package and non-subclass -- BOOOOOM , it crashes at runtime. 但是在运行时,实际调用的方法是从Calculator类形式开始的,现在发生的事情是,如果该方法实际上是从different packagenon-subclass BOOOOOM调用的,则该方法在运行时崩溃。

That is why it's not allowed. 这就是为什么它不允许。


Same concept applies when you are adding an extra checked exception . 添加额外的checked exception时,也适用相同的概念。 Compiler will again stop you. 编译器将再次阻止您。 But yes, you can add an extra Unchecked Exception though, because those are simply ignored by the Compiler . 但是可以,但是您可以添加一个额外的Unchecked Exception ,因为这些Unchecked Exception会被Compiler忽略。

From the Java Language Specification, §9.4 : 根据Java语言规范的第9.4节

Every method declaration in the body of an interface is implicitly public (§6.6). 接口主体中的每个方法声明都是隐式公共的(第6.6节)。

Every method declaration in the body of an interface is implicitly abstract, so its body is always represented by a semicolon, not a block. 接口主体中的每个方法声明都是隐式抽象的,因此其主体始终由分号(而不是块)表示。

It is permitted, but discouraged as a matter of style, to redundantly specify the public and/or abstract modifier for a method declared in an interface. 允许(但不鼓励使用样式)为接口中声明的方法冗余地指定public和/或abstract修饰符。

And from the JLS, §8.4.8.3 : JLS,第8.4.8.3节

The access modifier (§6.6) of an overriding or hiding method must provide at least as much access as the overridden or hidden method... 覆盖或隐藏方法的访问修饰符(§6.6)必须至少提供与覆盖或隐藏方法相同的访问权限...

Put the rules from those two sections together and the conclusion is that any method that is part of an interface implementation must be public . 将这两部分的规则放在一起,得出的结论是,作为接口实现一部分的任何方法都必须是public

Interfaces in Java are usage-contract of a class for its clients . Java中的接口是其客户端的类的使用合同。 So all their methods are public, and you can not apply more restriction on overridden methods. 因此,它们的所有方法都是公开的,您不能对覆盖的方法施加更多限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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