Author Topic:   Duplicate Method Names
Betty Reynolds
ranch hand
posted April 15, 2000 04:30 PM         
This question may have been asked before. If so, just refer me to the appropriate discussion, What I'm really looking for is some reference to the JLS to explain the results or a clear, understandable explanation. Thanks in advance.


code:

class Super
{
static String greeting() { return "Goodnight"; }
String name() { return "Richard"; }
}

class Sub extends Super
{
static String greeting() { return "Hello"; }
String name() { return "Dick"; }
}

public class TestB
{
public static void main(String[] args)
{
Super s = new Sub();
System.out.println("Test word formatting");
System.out.println(s.greeting() + ", " + s.name());
}
}


The result when you run this is "Goodnight, Dick". Why "Goodnight"?


Betty Reynolds
ranch hand
posted April 15, 2000 07:01 PM         
Sorry, just disregard. This example is from the JLS, in the section about hiding versus overriding for static methods.

maha anna
bartender
posted April 15, 2000 07:04 PM             
Betty, this means you got the concept cleared right?
regds
maha anna

Betty Reynolds
ranch hand
posted April 15, 2000 07:10 PM         
Yes, since greeting is static the reference at compile time is used to determine what method to invoke as opposed to name which is determined at run time.

maha anna
bartender
posted April 15, 2000 07:16 PM             
Yes. All static ones (var/methods)are determined at compile time itself. Also the 'private' ones (var/methods).
regds
maha anna

[This message has been edited by maha anna (edited April 15, 2000).]

Matt Claflin
greenhorn
posted April 16, 2000 05:15 PM             

Hi. It's not clear to me why "Goodnight Dick" would appear after running this. I'm having some trouble making the distinction between compile-time information checking and run-time information checking. Many certification exam questions require that one know when a problem will arise at compile time versus run time. Unfortunately, these are the questions I seem to consistently get wrong! Can you help me with the distinction? Thanks!

-Matt

Betty Reynolds
ranch hand
posted April 16, 2000 07:19 PM         
You're right. There are a lot of areas that require knowledge of what happens at compile-time vs. run-time and vice versa (i.e, casting, overriding/hiding, checked and unchecked exceptions, etc). My advice to you is to search on keywords such as "hidden", "cast", "polymorphism" etc. There were a lot of good discussions on these areas in this site and you can pick up a lot of knowledge here.

Of course, if you have the time and the motivation, the best source for this information is the JLS.

[This message has been edited by Betty Reynolds (edited April 16, 2000).]

Matt Claflin
greenhorn
posted April 17, 2000 09:05 AM             
Thanks, Betty - I'll give searching a shot!

-Matt

|