there is a clear explanation in JLS (chapter 6 I think)
anyway you can follow the extract below too..
<-- got this somewhere from the net -->
When Is an �Overridden� Method Not Really Overridden?
OK, so I admit it. The question posed in the title of this item is really a trick question. The goal of this item, though, is not to trick you, but rather to help you understand the concept of overriding methods. I�m sure that you have picked up a book or two that began explaining
Java by pointing out the three main concepts of object-oriented programming: encapsulation, inheritance, and
polymorphism. Understanding these three concepts is crucial to understanding the Java language. Understanding method overriding is crucial because it is a key part of inheritance.
Overriding an instance method is covered in Item 5. This item covers overriding static methods. If you didn�t know there was a difference, then this item (as well as Item 5) is for you. If you have started breaking out into a sweat and find yourself screaming �You can�t override a static method!,� then you may want to relax a bit and move on to the next item. But first, see if you can figure out what the output of the following example will be.
This example is taken from the Java Language Specification, section> 8.4.8.5:
class Super
{
static
String greeting()
{
return �Goodnight�;
}
String name()
{
return �Richard�;
}
}
class Sub extends Super
{
static String greeting()
{
return �Hello�;
}
String name()
{
return �Dick�;
}
}
class
Test {
public static void main(String[] args)
{
Super s = new Sub();
System.out.println(s.greeting() + �, � + s.name());
}
}
The output from running class Test is:
Goodnight, Dick
If you came up with the same output, then you probably have a good understanding of method overriding. If you didn�t, let�s figure out why. We will start by examining each class. Class Super consists of the methods greeting and name. Class Sub extends class Super and also has the greeting and name methods. Class Test simply has a main method.
In line 5 of class Test, we create an instance of class Sub. Make sure that you understand that even though variable s has a data type of class Super it is still an instance of class Sub. If that�s a bit confusing, then you can think of it this way: Variable s is an instance of class Sub that is cast to type Super. The next line (6) displays the value of the returned by s.greeting(), followed by the string �,� and the value returned by s.name(). The key question is �Are we calling the methods of class Super, or are we calling the methods of class Sub?� Let�s first figure out if we are calling class Super�s name method or class Sub�s name method. The name method in both the Super class and the Sub class is an instance method, not a static method. Because class Sub extends class Super and has a name method with the same signature as its parent class, the name method in class Sub overrides the name method in class Super. Because variable s is an instance of class Sub and class Sub�s name method overrides class Super�s name method, the value of s.name() is �Dick.�
We are half way there. Now we need to figure out if the greeting method is being called by class Super or by class Sub. Notice that the greeting method in both the Super class and the Sub class is a static method, also known as a �class� method. Despite the fact that the greeting method of class Sub has the same return type, the same method name, and the same method parameters, it does not override the greeting method in class Super. Because variable s is cast as type Super and the greeting method of class Sub does not override the greeting method of class Super, the value of s.greeting() is �Goodnight.� Still confused? Follow this rule: �Instance methods are overridden, static methods are hidden.� If you were the reader who was screaming �You can�t override a static method!,� you are absolutely right
Now you may be asking this: �What is the difference between hiding and overriding?� You may have not recognized it, but we actually just covered the difference in the Super/Sub class example. Using a qualified name can access hidden methods. Even though variable s is an instance of class Sub and the greeting method of class Sub hides the greeting method of class Super, we can still access the hidden greeting method by casting variable s to class Super. Overridden methods differ because they cannot be accessed outside of the class that overrides them. That is why variable s calls class Sub�s name method and not class Super�s name method.
This item is a short explanation of a sometimes confusing aspect of the Java language. Perhaps the best way for you to understand the difference between hiding a static method a overriding an instance method is for you to create a few classes similar to class Sub and class Super. Remember, instance methods are overridden and static methods are hidden. Overridden methods cannot be accessed outside of the class that overrides them. Hidden methods can be accessed by providing the fully qualified name of the hidden method.
Now that we understand that the answer to the question posed by the title of this item is �never,� I have a few more tidbits for you to keep in mind:
� It is illegal for a subclass to hide an instance method of its super class with its own static method of the same signature. This will result in a compiler error.
� It is illegal for a subclass to override a static method of its super class with its own instance method of the same signature. This will also result in a compiler error.
� Static methods and final methods cannot be overridden.
� Instance methods can be overridden.
� Abstract methods must be overridden.
<-- notes end here -->
[This message has been edited by Subramaniam Venkatesan (edited March 01, 2001).]