• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inner classes and inheritance  RSS feed

 
Jim Newton
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm wrestling with inner classes and so far inner classes are winning ...

In Java in a Nutshell (5th ed, p147), Flanagan writes thus, discussing the difference between inheritance hierarchy and containment hierarchy: "There should not be a problem if you refrain from creating naming conflicts, where a field or method in a superclass has the same name as a field or method in a containing class." This makes complete sense to me. He continues: "If such a naming conflict does arise, however, the inherited field or method takes precedence over the field or method of the same name in the containing class." My italics. This seems the opposite of everything I understand about inheritance and overriding. If ClassB extends ClassA and both have a field called aField, an instance method of ClassB that refers, unqualified to aField should get this.aField, not super.aField. Yes? So what is Flanagan saying? I have been tinkering around with some code intended to illustrate the consequences of the naming conflict he discusses. This is what I wrote and it seems to confirm my understanding of inheritance. Obviously I've misunderstood what Flanagan is saying , so can someone explain/post code which illustrates what he's talking about please?
Thankyou.





Output:

Class B 2
Class A 1
Class A
Class B
Class InnerB 3
Class B 2
Class A 1
Class A
Class B
Class A 1
Class A
Class InnerB
Class InnerInnerB 4
Class InnerB 3
Class B 2
Class A 1
Class A
Class B
Class A 1
Class A
Class InnerB
Class B 2
Class A 1
Class A
Class B
Class InnerInnerB
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure; I am tired and can't understand that Flanagan quote either.

But try here and use ctrl-f Puzzlers and find Bloch and Gafter's book. Page 180 has a list of meanings for overriding, hiding, shadowing, etc. It points out that a hidden member of a class is not inherited. That, along with the "puzzles" in the preceding chapter (page 157) will probably help you.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the third edition, the passage before this reads...
With the introduction of member classes, there are two separate hierarchies that must be considered for any class. The first is the classhierarchy, from superclass to subclass, that defines the fields and methods a member class inherits. The second is the containmenthierarchy, from containing class to contained class, that defines a set of fields and methods that are in the scope of (and are therefore accessible to) the member class.

The two hierarchies are entirely distinct from each other; it is important that you do not confuse them.

So I think he's talking about something like this...

Note that the class Nested2 extends Nested1. Therefore, it inherits meth() from its classhierarchy. In addition, Nested2 is also a member of Containing2, and therefore has access to the meth() defined in Containing2 -- its containmenthierarchy.

So the point is, in a conflict like this, the inherited method (from Nested1) takes precedence over the method of the same name in the containing class (Containing2). The reason has to do with scope -- not polymorphism. As explained in the book...
...inherited fields and methods are in the scope of the class that inherits them and take precedence over fields and methods by the same name in enclosing scopes.
 
Jim Newton
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your example really helps clarify the following paragraph.

Thankyou!
 
Jim Newton
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... and thanks to Campbell for the pointers. I've only got as far as Puzzle 8 so far, but I'll check out your references! Gotta love the Ranch ...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!