Re: inner class scope issues




Thanks for taking time to answer, Lew. Even though I must say I'm
still struggling.

Lew answered:
    public static class SomeInnerClass extends SomeSuperClass {

It is contradictory to name a static nested class with the word
"Inner".  In Java, "inner classes" are non-static member classes.

I know, and I expected your remark. But I wanted to stay consistent
with my first (erroneous) SSCCE.

Of course, the result is the same without "this.".

Wrong, sort of.  The reason it fails without 'this' is that the method
call does not have an owning instance.

This is subtly misleading (I didn't say incorrect, mind you!).
You don't have an instance when you're in a static context. Here there
is an instance which is "this", because the method is not static (even
though the nested class is).
Only, the present instance is an instance of the subclass.
I said your remark wasn't incorrect; indeed, the method doesn't have
an "owning instance" because this particlar instance does not own the
method... but I just wanted to point out the difference with "no
instance at all".
You know, to keep things clear for all the lurkers. :-)

#2) we need an instance, because the method is not static (and the
nested class is static): we have super, which is an instance.

Not completely correct.  'super' is this instance, not just any
instance.  'super' means "'this' taken as an instance of the
supertype".

Yes, I expected to be corrected on this. After writing this I tried
out the explicit cast version below, just to verify to myself that I
was going down the wrong path. But I was searching for different
mindsets.

The correct mindset seems to be to think about inheritance of private
members. So, to reformulate...
super.somePrivateMethod(); // works fine!
works because we are first accessing an instance of the nesting class,
then calling a private method upon it.
somePrivateMethod(); // compiler error
doesn't work because we're calling a method that doesn't exist ("this"
does not have this method, so this.somePrivateMethod() doesn't exist).

If this is correct, it means that the compiler error is misleading: it
speaks about an access issue, which taken at face value should be
resolved by nesting. I suppose the compiler simply provides the most
helpful message it can, and the scope-based message is generally the
most helpful.
In this particular case, it's not that helpful to say "you can't
access a private member" when on every other line you are! :-)

If I keep this point of view, the access should be ok because it's nested.
So I need to see a subclass as being a superclass, but *without* the
superclass's private members (not with them but with no access).

Not true.  The subclass still does have those private superclass
members, it just doesn't inherit them.

That doesn't help me. It's got them, but doesn't inherit them???
Confusing.
Either a class has a method, or it doesn't.
If it doesn't declare a method, the only way to have it is by
inheritance.

OTOH, it may be just a question of words. Do we really care if some
say...
"it doesn't have a method"
....and others say...
"it has a method but doesn't declare it and can't access it"?

The question of access to private members of an outer class is
orthogonal to inheritance, unrelated.

Well, that's more or less what the whole thread is about. :-)

--
JRobinss
.



Relevant Pages

  • Re: Confirm bad news
    ... nested class within the same enclosing class. ... class B {private int secret;} ... members have scope throughout the compilation ...
    (comp.lang.java.programmer)
  • Re: Need help with calling one class from another
    ... is an object within the other class (InventoryItem). ... The line you say doesn't work isn't accessing the private field, ... I admit, from the code you posted, it's not obvious to me why you have the nested class anyway. ... That said, the issue you're seeing is a basic rule about nested classes: the nested class can see "private" members of the containing class, but not the other way around. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Nested Class vs Inheritance
    ... > When is nested class more preferable that Inheritance? ... Using inheritance or nested class depends very much on what you want to ... class CWrapper: public CBaseWrapper { ...
    (comp.lang.cpp)
  • Re: Need help with calling one class from another
    ... isn't accessing the private field, ... value to the nested class anyway. ... the nested class can see "private" members of the containing class, ... private variables. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: error CS0621: virtual or abstract members cannot be private
    ... private virtual Doit ... It seems like a very strange pattern to me to think that a) _any_ code would be able to directly execute Derived's Doitimplementation, and also that b) the only code that theoretically could do so would be in the very class where Derived is declared (i.e. you will _never_ be in a situation where the Derived type's method is used by code that has knowledge only of the Base class, the usual reason we use polymorphism in the first place). ... Personally, to me it seems that even having a nested class inherit the containing class is a step in the wrong direction to start with, never mind the odd polymorphic construct. ...
    (microsoft.public.dotnet.languages.csharp)