简体   繁体   English

有什么方法可以禁止子类在Java中调用父类的公共方法?

[英]Is there any way to forbid the son class to call the public method of super class in java?

Is there any way to forbid the son class to call the public method of super class in java? 有什么方法可以禁止子类在Java中调用父类的公共方法?

For example 例如

public abstract class TObject{
    abstract public void quark();
}


public class Animal extends TObject{

       public void quark(){
           System.out.println("this is the animal");
       }

}

public class Dog extends Animal{
       @overide
       public void quark(){
           System.out.println("this is the animal");
           **super.quark();**
       }
} 

In this example, The Dog call the **super.quark();** in it's quark method.

But I don't want the Dog could call super.quark(); 但是我不希望狗可以调用super.quark(); and I also don't want to change the 而且我也不想更改

modifier of quark method in Animal to private. 将“动物”中的夸克方法修改为私有。 Is there any way to prevent this in compile? 有什么办法可以防止编译?

I have be confused couple of days, who can help me........ 几天我很困惑,谁能帮助我.......

The reason I do that is I met the similar problem in developing hudson scm plugin .I 我这样做的原因是我在开发hudson scm插件时遇到了类似的问题。

created the class which extends the SubversionSCM(the offical class). 创建了扩展SubversionSCM的类(正式类)。 I just wanted to 我只是想

override the public method of super class, then call super's public method back like 覆盖超类的公共方法,然后像

example. 例。 but the compile gave error.I don't konw why, how could it do? 但是编译产生了错误。我不知道为什么,怎么办? Dose java have 剂量java有

something like reflect way s to prevent this? 类似反射方式来防止这种情况?

No, by definition of public you can't stop the method from being called (whether from a derived class or anywhere else). 不,根据public的定义,您不能阻止该方法的调用(无论是从派生类还是在其他任何地方)。 You can of course stop it from being overridden (and thereby ensure that the syntax used to call it won't use super ;-) by making it final . 您当然可以通过将其设为final来阻止其被覆盖 (从而确保用于调用它的语法不会使用super ;-)。

No, there's no way to have a public method that subclasses can't call. 不,没有办法拥有子类无法调用的公共方法。 The best you can do is document this recommendation. 您能做的最好的就是记录此建议。

As a note, it's called a subclass or child class. 注意,它称为子类或子类。

The other 2 answers ( Alex & Matthew) are basically right. 其他两个答案( 亚历克斯马修)基本上是正确的。

  1. you can prevent the subclass from calling a public method from the super class 您可以防止子类从超类调用公共方法
  2. there is probably something wrong with your design if you do this. 如果这样做,您的设计可能存在问题。

The below Father class has two public methods named fatherMethod. 下面的父亲类具有两个名为父亲方法的公共方法。 Subclasses can call the fatherMethod using reflection (Thank You Matthew for pointing that out.) Without using reflection, subclasses can probably not call either fatherMethod method. 子类可以使用反射调用父亲方法(谢谢马修指出)。如果不使用反射,子类可能无法调用任何父亲方法。 Thus it is very similar to a private method. 因此,它与私有方法非常相似。

class Father
{
    private class Alpha { }

    private class Beta { }

    public void fatherMethod ( Alpha param )
    {
    }

    public void fatherMethod ( Beta param )
    {
    }
}

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

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