From: Jeff W. <jww...@ya...> - 2005-02-28 17:26:11
|
--- Hiroo Hayashi <hir...@co...> wrote: > I can hardly believe what you wrote. If override > does not work, the Java > environment is useless at all. How did you confirm > it? What happen if > you put print statement in your > calculateChecksum(Patch,int,int,int)? Defies logic doesn't it? I know I have to keep pinching myself. I've tested this numerous times both stepping through with a debugger and using print statements. Finally, I put together a really simple example that demonstrates this (See below). What I've found out is that it appears to be happening because Driver.calculateChecksum(Patch, int, int, int) is defined as a static method. The attached example has a main class (StaticOverride) along with two classes, Rectangle, which inherits from Object, and Square which inherits from Rectangle. Both Rectangle and Draw contain a static draw(int) method that prints a message int number of times. Rectangle has a non-static draw() method that calls the draw(int) method. The main class, StaticOverride creates an object of type Square and calls it's draw() method. Because Square.draw() is commented out, it calls Rectangle.draw() which in turn calls draw(int). You would think that because draw(int) is overridden in the Square class, that Square.draw(int) would be called but nope, Rectangle.draw(int) is called instead. If you make the draw(int) methods non-static in both the Rectangle and Square classes, then it works the way you'd expect and the Square.draw(int) method is called instead. (At least, that's what happens on my system). Go ahead and try it. If you get different results than I did, then something's flakey either with my system or my brain. Here's my example: ---------------------------- import java.util.*; public class StaticOverride { public static void main (String args[]) { Square aSquare = new Square(); aSquare.draw(); } } class Rectangle { int nbr = 2; void draw() { draw(nbr); } static void draw (int num) { for (int i = 0; i < num; i++) { System.out.println("Drawing a rectangle."); } } } class Square extends Rectangle { // void draw() { // draw(nbr); // } static void draw (int num) { for (int i = 0; i < num; i++) { System.out.println("Drawing a square."); } } } __________________________________ Do you Yahoo!? Yahoo! Mail - Easier than ever with enhanced search. Learn more. http://info.mail.yahoo.com/mail_250 |