简体   繁体   English

在java.lang.Object类中拥有受保护方法的目的是什么?

[英]What is the purpose of having protected methods in java.lang.Object class?

When every class in Java derives from java.lang.Object then what is the purpose of having protected methods in the Object class? 当Java中的每个类派生自java.lang.Object时,在Object类中拥有受保护方法的目的是什么? They will always have the same visibility as public methods.Is there any reason why the following two methods are protected in the OpenJDK implementation? 它们将始终具有与公共方法相同的可见性。在OpenJDK实现中,有任何理由可以保护以下两种方法吗?

    protected native Object clone() throws CloneNotSupportedException;

    protected void finalize() throws Throwable { }

Edit: The comment stating new Object.finalize() is the best answer I could have anticipated! 编辑:说明新的Object.finalize()的评论是我能预料到的最佳答案! ..Thanks ..谢谢

For those who are differentiating accessibility of protected and public 对于那些区分受保护和公共可访问性的人

  package pkg1;
   public class Parent{
    protected void fun(){}
     }

package pkg2;
public class child extends pkg1.Parent{
  void fun2()
   {  child ch=new child();
      ch.fun();   // Accesses protected method (For this class protected /public is irrelevant in terms of accessibility
      Parent p=new Parent();
      //p.fun(); //can't do this
   }
  }

Controlling "visibility" of elements in class is important. 控制课堂中元素的“可见性”非常重要。 (See language tutorial here ) (请参阅此处的语言教程

As a quick summary, consider: 作为快速摘要,请考虑:

  1. private things are only visible to the class they are in private事物只对他们所在的class可见
  2. no modifier/default are only visible to the class and the package the class is in no modifier/default只对class和类所在的package可见
  3. protected things are visible to packages , class AND to sub-classes packagesclasssub-classes可以看到protected东西
  4. public things are visible to package , class , sub-classes AND the world public事物对packageclasssub-classes和世界都是可见的

Yes it makes sanse as they will be visible for extending classes and in the same package but nowhere else. 是的,因为它们可用于扩展类和在同一个包中,但在其他任何地方都是可见的。 If aB extends Object and bC extends Object than aB cannot call bC#clone() method. 如果aB extends ObjectbC extends Object不是aB则不能调用bC#clone()方法。

The same is with finalize. 最终确定也是如此。 You can override it as it is protected, but you cannot call it from any possible context. 您可以在受保护的情况下覆盖它,但不能从任何可能的上下文中调用它。

protected methods / variables / .. are usually intended to be useful from subclasses only. protected方法/变量/ ..通常仅用于子类。

protected is also visible within the same package but that's typically not the intention since that case is already covered when leaving visibility at default (ie not defining one of private , protected or public ). protected也可以在同一个包中看到,但这通常不是意图,因为在默认情况下保持可见性(即没有定义privateprotectedpublic )时已经涵盖了这种情况。

So the typical intention of a protected method is either that subclasses can override that method to specialize the behavior or that they are being allowed to call that method (but outside classes are not - goal: encapsulation) 因此, protected方法的典型意图是子类可以覆盖该方法以专门化该行为,或者允许它们调用该方法(但外部类不是 - 目标:封装)

For example Object#finalize() does nothing per default but subclasses can (usually should not) do special cleanup operations in there when an object is garbage collected. 例如, Object#finalize()在默认情况下不执行任何操作,但是当对象被垃圾回收时,子类可以(通常不应该)在那里执行特殊的清理操作。

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

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