David,
You said:
MyNameIs() returns 2 different things depending on if SuperClass is extended or not, which seems ambiguous to me.
It's not ambigous at all, since we're not querying an instance of SuperClass when we say one.MyNameIs(); We are asking 'one' who it is, and it properly responds "i am one".
It doesn't return two different things depending on whether or not we've extended the SuperClass. It's because we've asked two different instances who they are.
If your real requirement is actually what you ask here:
Is there a way that I can explicitly say
-"Gimme the name of the class that extends me, null if there is none."
Then I think using a mechanism like Adam's is the only way. "me" in your case is always the SuperClass, but the class has no idea WHO has extended it, unless we tell it to keep track of that. A child knows who its parent class is because of the keyword
extends, but a parent class does not track its children. If this were built into the language, it would probably have to be a new keyword, something like
sires or perhaps
isParentOf p.s.- Your second gimme:
"Gimme the name of my class, regardless if I'm extended or not"
is what the forName() method gives you. It gives the name of 'my class' ie: me, myself and I, what is my name, I don't care if I'm a bastard child or not.
p.p.s- Your code posted won't run. sc isn't declared before you try to 'out' it.
produces:
Maybe *this* is what you meant? Here we see that a base class reference, that is holding a child class, can still properly resolve the eternal question: WhoAmI?