NoSuchMethodException accessing public method in protected subclass

B

Brian J. Sayatovic

I have an abstract base class, and a subclass that is nested in
another unrelated class, like this:

public abstract class Base {
public abstract String getName();
}

public class AnotherClass {
protected static class Subclass extends Base {
public String getName() {
return "Subclass Name";
}
}
}

At runtime, I retrieve a list of AnotherClass instances. I want to
iterate through each, casting them to Base, and get their names. Now,
I'm doing this via Struts' taglibs using the logic:iterate and
bean:write tags. Internally, the bean:write tag uses reflection with
the JavaBean rules to look for a "getName" method corresponding to the
"name" property I'm asking Struts to write out.

In practice, through, I get a NoSuchMethodException. As soon as I
change the protection of AnotherClass to public (from protected), it
works.

Now, as I understand it, using straight Java code, I can get an
instance of AnotherClass and call getName() on it because its parent
class, Base, guarantees that getName() is public. Is this not true?

If it is true, then why doesn't reflection honor this as well?

FWIW, I'm using IBM's 1.3.1 JVM (inside WebSphere) on Windows XP.

Regards,
Brian.
 
C

Chris Smith

Brian said:
public abstract class Base {
public abstract String getName();
}

public class AnotherClass {
protected static class Subclass extends Base {
public String getName() {
return "Subclass Name";
}
}
}
[...]

Now, as I understand it, using straight Java code, I can get an
instance of AnotherClass and call getName() on it because its parent
class, Base, guarantees that getName() is public. Is this not true?

It's not true. AnotherClass is not related to Base. You've declared
Subclass in the scope of AnotherClass, but they aren't related by
inheritance at all.

I'm puzzled as to why making Subclass public would fix this. It
shouldn't, and I suspect there's something fishy going on in code that
you haven't posted.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
B

Brian J. Sayatovic

Chris Smith said:
Brian said:
public abstract class Base {
public abstract String getName();
}

public class AnotherClass {
protected static class Subclass extends Base {
public String getName() {
return "Subclass Name";
}
}
}
[...]

Now, as I understand it, using straight Java code, I can get an
instance of AnotherClass and call getName() on it because its parent
class, Base, guarantees that getName() is public. Is this not true?

It's not true. AnotherClass is not related to Base. You've declared
Subclass in the scope of AnotherClass, but they aren't related by
inheritance at all.

I'm puzzled as to why making Subclass public would fix this. It
shouldn't, and I suspect there's something fishy going on in code that
you haven't posted.

It's because in my haste, I mispoke. LEt me rephrase the misleading paragraph:

Now, as I understand it, using straight Java code, I can get an
instance of *AnotherClass.Subclass* and call getName() on it because its parent
class, Base, guarantees that getName() is public. Is this not true?

Regards,
Brian.
 
C

Chris Smith

Brian said:
It's because in my haste, I mispoke. LEt me rephrase the misleading paragraph:

Now, as I understand it, using straight Java code, I can get an
instance of *AnotherClass.Subclass* and call getName() on it because its parent
class, Base, guarantees that getName() is public. Is this not true?

Yes, you can obtain a reference to an instance of AnotherClass.Subclass.
However, you can't declare a reference with that type, unless you are
either in the same package as AnotherClass, or writing code in a
subclass of it.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top