But, if fieldGetter() is now part of Child and Parent was never actually instantiated, then how is Parent.field available for Child.fieldGetter() to read? In fact, how does Parent.field exist at all?
Paul Clapham wrote:Try not to get the access rules (private, public etc) mixed up with the inheritance rules. In your example: A Child object is a Parent object, because Child extends Parent. You'll see this relationship described as "IS-A" when people are talking about inheritance, e.g. a Child IS-A Parent. And since a Child object is a Parent object, it contains the field named "field" from the Parent declaration. It's true that none of Child's code has access to that field, but it's there nevertheless.
Paul Clapham wrote:As for where things are stored in memory: You're really not supposed to ask that question, because it's left up to the implementers of the JVM to decide that. The Java Language Specification just defines how the language works, not how it's implemented. However I'll say that a reasonable implementation of a JVM (i.e. one that I would write, ha ha) would keep all of Child's data members in the same place and not scatter them into one part for Child-only members, one part for Parent-only members, and one part for Object-only members (remember that Parent extends Object). So there's one block of memory which contains the members declared by Object, the members declared by Parent, and the members declared by Child.
I suppose an implementation which used three blocks of memory with pointers keeping them together would be conceivable (of course it IS conceivable because you already conceived of it), but it would use more memory and be harder to navigate and make garbage collection more difficult, which is why I think the actual JVM implementers and me are on the same page.
nick woodward wrote:parent is instantiated because the child IS-A parent as others have said. in the same way that an object is instantiated too - not as a separate 'thing', but as the child itself.
i'm pretty sure the Head First Java book has a good description of this with diagrams. in fact i've just checked - page 214 'get in touch with your inner object'.
Grant Robertson wrote:
nick woodward wrote:parent is instantiated because the child IS-A parent as others have said. in the same way that an object is instantiated too - not as a separate 'thing', but as the child itself.
i'm pretty sure the Head First Java book has a good description of this with diagrams. in fact i've just checked - page 214 'get in touch with your inner object'.
Thanks, I took a look at my copy of Head First Java and this at least explains enough for me to now trust that all the parts of Parent are truly embedded within Child. Of course, now that I think of it, how could they not be and have polymorphism still work.
So, whether a particular JVM uses only one copy each of all these common parts with lots of pointers or an actual separate copy in each part of the heap reserved for the methods and static fields common to an entire class is now rather moot. All I need to care about is that ALL the parts of the Parent are treated as if they are embedded (however the JVM decides to do so) within the Child. It is just that either the compiler or the JVM treats the private parts of Parent as if they are not actually part of Child whenever Child is being accessed through a variable that is declared with the Child datatype. And that either the compiler or the JVM treats the private parts of Parent as accessible, and ignores the parts of the object that come from Child, whenever that object is accessed through a variable that has been declared with the Parent datatype.
I think I can call this question "Resolved."
Thanks,
Grant
P.S. The reason I did not see that description in my Head First Java book is that all the extra crap in those books drives me crazy. I have moderate Asperger's and I find it impossible to ignore all those stupid cartoons. So I stopped reading it and got a different book. OK, lots of different books. I'm also a picky-ass technical-writer and bad grammar or bad explanations also drive me nuts.
nick woodward wrote:You may enjoy C. Thomas Wu as an author. Very 'to the point' and quite an academic style - apparently a negative in most reviews, but something I like.