Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>No. They don't inherit it. </p> <p>The fact some other class may use it indirectly says nothing about inheritance, but about encapsulation. </p> <p>For instance:</p> <pre><code>class Some { private int count; public void increment() { count++; } public String toString() { return Integer.toString( count ); } } class UseIt { void useIt() { Some s = new Some(); s.increment(); s.increment(); s.increment(); int v = Integer.parseInt( s.toString() ); // hey, can you say you inherit it? } } </code></pre> <p>You can also get the value of <code>count</code> inside <code>UseIt</code> via reflection. It doesn't means, you inherit it. </p> <p><strong>UPDATE</strong> </p> <p>Even though the value is there, it is not inherited by the subclass.</p> <p>For instance a subclass defined as:</p> <pre><code>class SomeOther extends Some { private int count = 1000; @Override public void increment() { super.increment(); count *= 10000; } } class UseIt { public static void main( String ... args ) { s = new SomeOther(); s.increment(); s.increment(); s.increment(); v = Integer.parseInt( s.toString() ); // what is the value of v? } } </code></pre> <p>This is exactly the same situation as the first example. The attribute <code>count</code> is hidden and <strong>not</strong> inherited by the subclass at all. Still, as DigitalRoss points out, the value is there, but not by means on inheritance. </p> <p>Put it this way. If your father is wealthy and gives you a credit card, you can still buy thing with his money, but doesn't mean you have <strong><em>inherited</em></strong> all that money, does it? </p> <p><strong>Other update</strong></p> <p>It is very interesting though, <a href="https://stackoverflow.com/questions/4716040/does-subclasses-inherit-private-fields/4716140#4716140">to know why the attribute is there.</a></p> <p>I frankly don't have the exact term to describe it, but it's the JVM and the way it works that loads also the "not inherited" parent definition. </p> <p>We could actually change the parent and the subclass will still work.</p> <p><a href="https://gist.github.com/OscarRyz/37fab40a68fa3e5051c1fb0b8c55b389" rel="nofollow noreferrer">For instance</a>:</p> <pre><code>//A.java class A { private int i; public String toString() { return ""+ i; } } // B.java class B extends A {} // Main.java class Main { public static void main( String [] args ) { System.out.println( new B().toString() ); } } // Compile all the files javac A.java B.java Main.java // Run Main java Main // Outout is 0 as expected as B is using the A 'toString' definition 0 // Change A.java class A { public String toString() { return "Nothing here"; } } // Recompile ONLY A.java javac A.java java Main // B wasn't modified and yet it shows a different behaviour, this is not due to // inheritance but the way Java loads the class Output: Nothing here </code></pre> <p>I guess the exact term could be found here: <a href="http://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.4.8" rel="nofollow noreferrer">The JavaTM Virtual Machine Specification</a></p>
    singulars
    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.
 

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