Tuesday, 15 May 2012

subclass - Why is access to protected member in Java implemented as it does? -


Questions about access to protected members in Java were already asked and answered many times, for example :

But I do not understand why it has been implemented in this way, see the explanation from "Java Programming Language" (4th Ed.):

" The logic behind this is: Each subclass increases the contract of the superclass and in some way, suppose that a subclass, a As part of an extended contract, as a part of the extended contract, the values ​​of the protected members of the super-class interfere. If a separate subclass can reach the protected members of the first object, then sub-classes can tamper with those methods which The sub-class will break the contract and it should not be allowed. "

Well, it is clear, but consider this legacy structure (extract some code):

  Package Type Cage 1; Public category A {protected intestine x; } Package package 2; Public class B extends a {public static zero main (string [] args) sub subclass = new c (); Subclass.x = 7; // Any obstacle can be broken here - ?? }} Class extends cb {// class which protects the value of x protected member ...}   

Here subclass.x = 7 is a valid statement that is now What is the contract of a C I am losing?

Edited (Added): Should not I apply the quoted logic in this situation? If we were working with only one package, then no restriction exists at all. So perhaps the straight legacy chain is treated in a simplified manner, which means that the super class should know what it is doing ... "post-text" itemprop = "text ">

This is ultimately about the following contracts, as stated in your posted quote. If you are really worried that no contract reads, then there is a defensive programming solution for this, which introduces verification on revision.

This means that the code posted by you can break the contract; This, however, could not:

  public class A {Private int x; Protected last zero setX (int x) is sold illegally Exception {if (x & lt; 0) throwing new illegal waste concept ("x can not be negative"); SubValidateX (x); This.x = x; } / ** * Subclasses that want to provide additional verification, this method should be overridden / protected void subValidateX (int x) {// unverified no-op}}   

Here, I did three main things:

  1. I have made x private, so it can only be assigned from within A ( Absolutely excluding things like reflection)),
  2. I created setter last which prevents subclass from overriding it and Removes the Recognition, and
  3. I have the protected method to ensure by subclasses that addition to x x <> To include things like negative integer, because my verification was already examined.

    There are many good resources to design for heritage in Java, especially when it comes to super-protective protection. Like my example above - Working API Programming I recommend watching them on the engine.

    Eventually, however, the developer writing a subclass needs to be responsible for reading the documentation, especially when you are in the interface implementation.

No comments:

Post a Comment