Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can access A's version of <code>x</code> like this:</p> <pre><code>((A)this).x </code></pre> <p>as long as <code>x</code> wasn't declared <code>private</code> in class <code>A</code>. I've just tested it.</p> <p>Note that for fields, there is no overriding (as there is for methods). Thus, for an object of class C, there will be three <code>x</code> fields, but two of them can't be accessed normally because they are hidden by the other field named <code>x</code>. But casting the object as above will allow you to get at it, if it would have been visible if not hidden.</p> <p>I think it is very poor practice to declare fields of the same name in a class and its subclasses. It's confusing. It can happen legitimately if, say, you have a class <code>A</code> and you later change the implementation of <code>A</code> and add a new private field <code>z</code>; in that case, it may not be possible to make sure no subclasses of <code>A</code> already have a field <code>z</code>, since you don't even always know what all the subclasses are (if <code>A</code> is a class you've distributed publicly, for instance). I think it's for that reason that Java allows you to have fields of the same name, and why the hiding rules are the way they are, because it allows things like this to work without breaking all the other subclasses. Other than that, though, I recommend <em>not</em> having fields of the same name in superclasses and subclasses. Perhaps if they're all <code>private</code> it might be OK, though. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload