Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Are static methods are overriden?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Are static methods are overriden??" Watch "Are static methods are overriden??" New topic
Author

Are static methods are overriden??

Narasimha Rao B.
Ranch Hand

Joined: Aug 26, 2002
Posts: 205
Hi,
Somewhere i read that, static methods will not be overriden, because static methods will not be inherited even they are public. Then, observe the below programme,
--------
1class P {
2 static void printS1(){System.out.print("P.printS1 ");}
3 void printS2() {System.out.print("P.printS2 ");}
4 void printS1S2(){printS1();printS2();}
5}
6public class D extends P {
8 void printS2(){System.out.print("Q.printS2 ");}
9 void printS1S2(){printS1();printS2();}
10
11
12 public static void main(String[] args) {
13 new D().printS1S2();
14}}
output - P.printS1 Q.printS2
--------------
In line number 9 of subclass D, i am invoking the method called, printS1(), which is a static method declared in the super class. If static methods are not inherited, then how the above progrmme is compiling and giving the below output.P.printS1 Q.printS2

Thanks,
Narasimha.


Narasimha
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
Who says that static methods are not inherited!!! Only methods that are marked with the private accecibilty are not inherited.
Static methods are not overriden, they are hidden. This means that the method which is actually invoked at runtime is based on the type of the reference, not on the type of the object instance.


- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. - What truth? - That there is no spoon!!!
Narasimha Rao B.
Ranch Hand

Joined: Aug 26, 2002
Posts: 205
Hi Vicken,
Thanks a lot for you quick and also nice example. I think i misunderstood the concept of hiding the method of the super class in the subclass, with overriden principle.
Thanks once agin,
Narasimha.
Liz Brown
Ranch Hand

Joined: Oct 22, 2002
Posts: 112
Viken-
In your code-
Parent p1 = new Child();-->why do you use Child to create Parent p1 obj?
//below 2 i can understand is normal way to instantiate the objs.
Parent p2 = new Parent();
Child c = new Child();
can you please explain the 1st statement? i get confused here sometimes.
thanks.
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
Originally posted by shivan B:
Viken-
In your code-
Parent p1 = new Child();-->why do you use Child to create Parent p1 obj?
//below 2 i can understand is normal way to instantiate the objs.
Parent p2 = new Parent();
Child c = new Child();
can you please explain the 1st statement? i get confused here sometimes.
thanks.

It is called polymorphism, you can find more about it here
Liz Brown
Ranch Hand

Joined: Oct 22, 2002
Posts: 112
thanks! that was a cool explaination
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Are static methods are overriden??