From: Jeff W. <jww...@ya...> - 2005-02-24 16:40:14
|
--- Joe Emenaker <jo...@em...> wrote: > Date: Thu, 24 Feb 2005 03:01:42 +0000 > From: Joe Emenaker <jo...@em...> > To: Jeff Weber <jww...@ya...> > Subject: Overriding calculateChecksum > > I came across this in your DM5 drivers.... > > /** Calculates the checksum for the DM5 by calling > * this.calculateChecksum(Patch patch, int start, int end, int offset). This > * needs to be included to override the version in the Driver class. > */ > protected void calculateChecksum(Patch p) { > calculateChecksum(p, checksumStart, checksumEnd, checksumOffset); > } > > Actually, I don't think you have to include this. If > you delete it, the > /un-overridden/ *calculateChecksum(Patch)* in Driver > should /still/ call > your /overridden/ version of > *calculateChecksum(Patch,int,int,int). > > One of the nice things about inheritance..... > Funny you should mention this. I was reviewing my code and came across this just last week and I thought the same thing. When I removed it I found that it actually does not call the overridden method. It calls the method in the Driver class. I used a debugger to confirm it. I don't understand why this happens. Maybe it's just something flakey with my system (OSX). __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com |
From: Hiroo H. <hir...@co...> - 2005-02-28 00:17:22
|
Jeff, Jeff> Funny you should mention this. I was reviewing my code Jeff> and came across this just last week and I thought the Jeff> same thing. When I removed it I found that it actually Jeff> does not call the overridden method. It calls the Jeff> method in the Driver class. I used a debugger to Jeff> confirm it. I don't understand why this happens. Maybe Jeff> it's just something flakey with my system (OSX). 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)? -- Hiroo Hayashi |
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 |
From: Rib R. <ri...@gm...> - 2005-02-28 17:53:19
|
That sounds right. If it's a static method there's no this pointer, so there would be no way to figure out which class's method to call at runtime. I don't know the language spec, but since Eclipse gives a warning whenever a static method is called through an object, I'd guess that all static methods are statically resolved at compile time. On Mon, 28 Feb 2005 09:25:58 -0800 (PST), Jeff Weber <jww...@ya...> wrote: > --- 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 > > > ------------------------------------------------------- > SF email is sponsored by - The IT Product Guide > Read honest & candid reviews on hundreds of IT Products from real users. > Discover which products truly live up to the hype. Start reading now. > http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click > _______________________________________________ > Jsynthlib-devel mailing list > Jsy...@li... > https://lists.sourceforge.net/lists/listinfo/jsynthlib-devel > |
From: denis q. <dqu...@fr...> - 2005-02-28 18:48:29
|
yes, that's why singleton is preferred to static methods. A singleton class can use inheritance. -- Denis > That sounds right. If it's a static method there's no this pointer, so > there would be no way to figure out which class's method to call at > runtime. I don't know the language spec, but since Eclipse gives a > warning whenever a static method is called through an object, I'd > guess that all static methods are statically resolved at compile time. > > > On Mon, 28 Feb 2005 09:25:58 -0800 (PST), Jeff Weber > <jww...@ya...> wrote: > >>--- 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 >> >> >>------------------------------------------------------- >>SF email is sponsored by - The IT Product Guide >>Read honest & candid reviews on hundreds of IT Products from real users. >>Discover which products truly live up to the hype. Start reading now. >>http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click >>_______________________________________________ >>Jsynthlib-devel mailing list >>Jsy...@li... >>https://lists.sourceforge.net/lists/listinfo/jsynthlib-devel >> > > > > ------------------------------------------------------- > SF email is sponsored by - The IT Product Guide > Read honest & candid reviews on hundreds of IT Products from real users. > Discover which products truly live up to the hype. Start reading now. > http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click > _______________________________________________ > Jsynthlib-devel mailing list > Jsy...@li... > https://lists.sourceforge.net/lists/listinfo/jsynthlib-devel > > |
From: Joe E. <jo...@em...> - 2005-03-01 11:54:48
|
Jeff Weber wrote: > 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. > > That's not supposed to even be possible. From: http://java.sun.com/docs/books/tutorial/java/javaOO/override.html .... "a subclass cannot override methods that are declared |static| in the superclass. In other words, a subclass cannot override a class method." However, it goes on to say that: "A subclass can /hide/ a |static| method in the superclass by declaring a |static| method in the subclass with the same signature as the |static| method in the superclass." But it seems like hiding would be the opposite problem than what you're getting now. - Joe |
From: Joe E. <jo...@em...> - 2005-03-01 23:28:02
Attachments:
smime.p7s
|
Jeff Weber wrote: >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. > > I found some discussion about this on the java newsgroups... so it seems to be "the way it is". I find this very disappointing. The potential bugs that this invites would be difficult enough to track down that it's almost not worth using the static modifier at all. At the very least, I'd propose that we make all static methods and properties *final*. That way, you explicitly can't override them. That would ensure that a problem like this doesn't happen. - Joe |
From: Hiroo H. <hir...@co...> - 2005-03-02 06:34:06
|
Hi Jeff, Jeff> What I've found out is that it appears to be happening Jeff> because Driver.calculateChecksum(Patch, int, int, int) Jeff> is defined as a static method. You are right. The Java Programming Language Third Edition, Chapter 3 Extending Classes, 3.3.5 Hiding Static Members starts by the following sentence. Static members within a class cannot be overridden, they are always hidden --- whether a field or a method. I completely did not understand this rule. It was my fault to make Driver.calculateChecksum(Patch, int, int, int) static. I'll make it non-static method. -- Hiroo Hayashi |
From: <jo...@em...> - 2005-03-03 04:03:16
|
> I completely did not understand this rule. It was my fault to make > Driver.calculateChecksum(Patch, int, int, int) static. I'll make it > non-static method. Do you think we should also make as many static members "final" as well, to prevent this kind of thing from happening again? After all, I'll bet it took Jeff a lot of needless head-scratching to find this problem. - Joe |
From: Hiroo H. <hir...@co...> - 2005-03-03 04:10:47
|
joe> Do you think we should also make as many static members "final" as well, joe> to prevent this kind of thing from happening again? After all, I'll bet it joe> took Jeff a lot of needless head-scratching to find this problem. As far as I see the calculateChecksum(Patch,int,int,int) was the only static method in the classes which a synth driver extends. -- Hiroo Hayashi |
From: Hiroo H. <hir...@co...> - 2005-03-03 04:07:28
|
I've checked in the fixes in the main branch. On Wed, 02 Mar 2005 00:34:08 -0600 Hiroo Hayashi <hir...@co...> wrote: Hiroo> The Java Programming Language Third Edition, Chapter 3 Extending Hiroo> Classes, 3.3.5 Hiding Static Members starts by the following sentence. Hiroo> Hiroo> Static members within a class cannot be overridden, they are always Hiroo> hidden --- whether a field or a method. Hiroo> Hiroo> I completely did not understand this rule. It was my fault to make Hiroo> Driver.calculateChecksum(Patch, int, int, int) static. I'll make it Hiroo> non-static method. -- Hiroo Hayashi |