• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

K&B Static Method Redifinition Problem..

 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a code snippet from K&B:



And the output is : a a a d

Question 1:

When the doStuff is called inside the loop it prints a whereas when it is outside the loop it prints d! And both the instances that invoke the method are a Dog annonymous Object.. Why is that..

Question 2:What does really redifinition of static method mean? When it has a different body in the subclass than wont that be invoked on runtime? What is the use of different body implementation of the static method in subclass and no use of it? I cant understand this concept..


Also I have made some changes to the code in the book.. In the book the methods in the superclass is static void doStuff() and in the subclass is static void dostuff(). So there is no question of redefiniton when both the methods are of different names. Is that an error in the book??

Any descriptive answers would be appreciated..
Thanks !!
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this might help...hiding static methods
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Somnath

Overriding(or Hiding) rules:
1) Static members (methods and variables) invoked based on the type of Object reference, no matter of class instance
2) Same rule apply for non static variables
3) Non static methods invoked based on instance of the class not by the type of object reference

For any static or non static member overriding(or hiding), purpose is same: override(or hide) and make use of the one that is required at runtime

Thanks,
Alpesh
[ August 18, 2008: Message edited by: Alpesh Vesuwala ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic