为什么Java允许增加子类中受保护方法的可见性? [英] Why Java allows increasing the visibility of protected methods in child class?

查看:172
本文介绍了为什么Java允许增加子类中受保护方法的可见性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 抽象类Base {
protected abstract void a();
}

class Child extends Base {
@Override
public void a(){
//为什么这个有效的
}
}

为什么我们不能降低可见性,但可以增加? p>

此外,我还需要实现模板模式,其中可见的公共方法只能是基类。



示例:

 抽象类Base {
public void callA(){
/ /做一些重要的东西
a();
}

protected abstract void a();
}

class Child extends Base {
@Override
public void a(){
//为什么这个有效的
}
}

现在如果java允许增加可见性,那么有两种方法可见公开? / p>

我知道界面是一个解决方案,但还有其他一些出路?

解决方案

为了降低可见性是不允许的,已经在其他响应中解释了(它会破坏父类的合同)。



但是为什么允许增加方法的可见性?首先,它不会违约,所以没有理由不允许。有时候,当孩子阶段有一个不受保护的方法是有意义的时候,它可以很方便。



其次,不允许这样做会产生不可能的副作用有时候扩展一个类并同时实现一个接口:

  interface Interface1 {
public void method() ;
}

public class Parent {
protected abstract void method();
}

public class Child extends父实现Interface1 {
@Override
public void method(){
}
//这将如果父类中的method()的可见性无法增加,则不可能。
}

关于你的第二个问题,你什么都不做。你必须相信执行子类的人没有做任何打破你的实现的事情。即使java不允许增加可见性,仍然无法解决您的问题,因为可以创建一个具有不同名称的公共方法来调用抽象方法:

  class Child extends Base {
@Override
protected void a(){

}

public void a2(){
a(); //这将有同样的问题,允许增加可见性。
}
}


abstract class Base{
      protected abstract void a();
}

class Child extends Base{
      @Override
      public void a(){
          //why is this valid
      }
}

Why is that we can't reduce the visibility but can increase it?

Also I need to implement Template pattern in which the public methods visible can only be of base class.

Example:

abstract class Base{
      public void callA(){
      //do some important stuff
      a();
      }

      protected abstract void a();
}

class Child extends Base{
      @Override
      public void a(){
          //why is this valid
      }
}

Now if java allows to increase visibility then there are two methods visible publicly??

I know interface is one solution but is there some other way out???

解决方案

Why decreasing visibility is not allowed is already explained in other responses (it would break the contract of the parent class).

But why it is allowed to increase the visibility of a method? First, it would not break any contract, so there is no reason to not allow it. It can be handy sometimes, when it makes sense in the child class for a method to not be protected.

Second, not allowing it could have the side effect of making impossible sometimes to extend a class and implement an interface at the same time:

interface Interface1 {
   public void method();
}

public class Parent {
   protected abstract void method();
}

public class Child extends Parent implements Interface1 {
   @Override
   public void method() {
   }
   //This would be impossible if the visibility of method() in class Parent could not be increased.
}

About your second question, you can do nothing about it. You have to trust that the person who implements the child class doesn't do anything that breaks your implementation. Even if java wouldn't allow to increase visibility, that would still not fix your problem, because a public method with a different name could be created that calls the abstract method:

class Child extends Base{
      @Override
      protected void a(){

      }

      public void a2() {
           a(); //This would have the same problems that allowing to increase the visibility.
      }
}

这篇关于为什么Java允许增加子类中受保护方法的可见性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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