Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #30629 > unrolled thread
| Started by | Stone Zhong <stone.zhong@gmail.com> |
|---|---|
| First post | 2015-01-04 23:52 -0800 |
| Last post | 2015-01-05 22:12 +0000 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.java.programmer
Why base class's static method shows up in derived class? Stone Zhong <stone.zhong@gmail.com> - 2015-01-04 23:52 -0800
Re: Why base class's static method shows up in derived class? Patrick Roemer <sangamon@netcologne.de> - 2015-01-05 13:07 +0100
Re: Why base class's static method shows up in derived class? v_borchert@despammed.com (Volker Borchert) - 2015-01-05 22:12 +0000
| From | Stone Zhong <stone.zhong@gmail.com> |
|---|---|
| Date | 2015-01-04 23:52 -0800 |
| Subject | Why base class's static method shows up in derived class? |
| Message-ID | <e8dd9f53-8064-4e8b-bedf-809bddecfe8c@googlegroups.com> |
Hi there,
Can anyone share some experiences with this weird behavior:
With the code snipped below:
for (Method method: HttpClientContext.class.getMethods()) {
if (method.getName().equals("create")) {
System.out.printf("%s%n", method);
}
}
I got output:
public static org.apache.http.client.protocol.HttpClientContext org.apache.http.client.protocol.HttpClientContext.create()
public static org.apache.http.protocol.HttpCoreContext org.apache.http.protocol.HttpCoreContext.create()
HttpClientContext derived from HttpCoreContext. Both class has a static method "create" with no parameter.
Why HttpCoreContext.create shows up as HttpClientContext's method? I am expecting only "public static org.apache.http.client.protocol.HttpClientContext org.apache.http.client.protocol.HttpClientContext.create()" to show up, am I missing anthing?
Thanks,
Stone
[toc] | [next] | [standalone]
| From | Patrick Roemer <sangamon@netcologne.de> |
|---|---|
| Date | 2015-01-05 13:07 +0100 |
| Message-ID | <m8duq6$cim$1@newsreader4.netcologne.de> |
| In reply to | #30629 |
Responding to Stone Zhong:
> With the code snipped below:
>
> for (Method method: HttpClientContext.class.getMethods()) {
> if (method.getName().equals("create")) {
> System.out.printf("%s%n", method);
> }
> }
>
>
> I got output:
> public static org.apache.http.client.protocol.HttpClientContext org.apache.http.client.protocol.HttpClientContext.create()
> public static org.apache.http.protocol.HttpCoreContext org.apache.http.protocol.HttpCoreContext.create()
>
>
> HttpClientContext derived from HttpCoreContext. Both class has a static
> method "create" with no parameter.
>
> Why HttpCoreContext.create shows up as HttpClientContext's method? I am
> expecting only "public static org.apache.http.client.protocol.HttpClientContext
> org.apache.http.client.protocol.HttpClientContext.create()" to show up,
> am I missing anthing?
Class#getMethods() returns (public) methods inherited from superclasses
and interfaces as well. Static methods from direct superclasses are
inherited just as instance methods. So in general it's fine that static
methods of the superclass show up in the output of #getMethods().
What puzzles me in this specific case is that both methods have the same
signature. To my understanding, according to JLS8 ยง8.4.8, the
superclass' #create() method should not be inherited because of the
subsignature relationship...
Best regards,
Patrick
[toc] | [prev] | [next] | [standalone]
| From | v_borchert@despammed.com (Volker Borchert) |
|---|---|
| Date | 2015-01-05 22:12 +0000 |
| Message-ID | <m8f280$2al$2@Gaia.teknon.de> |
| In reply to | #30629 |
Stone Zhong wrote: > [ ... ] am I missing anthing? Yes - Class.getDeclaredMethods() -- "I'm a doctor, not a mechanic." Dr Leonard McCoy <mccoy@ncc1701.starfleet.fed> "I'm a mechanic, not a doctor." Volker Borchert <v_borchert@despammed.com>
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web