• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Are static methods are overriden??

 
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Narasimha Rao B.
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks! that was a cool explaination
 
Looky! I'm being abducted by space aliens! Me and this tiny ad!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic