Win a copy of High Performance Python for Data Analytics this week in the Python forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

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: 9674
42
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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic