• 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

non-override, static and private is different

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We already know that static or private method will not be considered as overriden, but there are still some difference between them:

By switch the comments on //line1 and //line2, it seems that
private is really not override, but
static seems still consider as override, thought(if we comments the throws exception clause to make it compile) the amethold is still invoke the base version.
Can anyone explain the difference?

[This message has been edited by shadow liu (edited May 08, 2001).]
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can override static methods, as long as you keep the method static in the subclass.
In your example (as commented), the amethod() method in the parent is private and is not overridden. If you comment out the other line and make the parent method public static, then the child method is overriding the parent method.
However, in the call you make (b.aa()), it is the parent's aa() method which is being called, which then invokes the parent's amethod() -- regardless of whether the method has been overridden by the child or not.
If, on the other hand, you override the aa() method in the child (instead of simply inheriting it, as the code you've written does), then it will be the child's amethod() which is called.
Is this any help?
 
Scott Appleton
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Upon further review, it does appear that the static method is not being overridden -- if the parent calls the method, then it's the parent's method which is being executed, and likewise for the child. The guidelines I've read only state that static methods may not be overridden by non-static methods, but it's not clear to me how the static method is being overridden at all.
 
shadow liu
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To make my question clear:
Since 'static version' invoke the base amethod, so static method is not consider as override.
But when 'static version' throw exception, it need obey the override rules.
WHY?
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In order to override anything there has to be late binding. However static stuff is resolved at compile time, and therefore can not do late binding, they are already stuck in place.
What CAN happen is that the super class can have a static method that has the same name as a static method of the child class. In this case the child static class is said to "hide" the super class method. YOu can not get at the super class static method directly.
You can tell the difference by how they need to be called.
If you try to access the static super method by using
super.amethod() or this.amethod() you get a compile error. but if it is not static that syntax works just fine.
Instead for the static methods you need to name the class whose static method that you want to invoke.
b39.amethod() and b40.amethod() invoke the correct static method of each class.
Of course you CAN name a particular instance of a given class and get at the static methods that way (such as above - which is a confusing approach at best cuz it is not obvious that you are calling a static method).
The variable b is a b39 and has all the compile time characteristics -like the variables of b39 and the static methods of b39 so when you say "b.amethod()" since b is a b39 the amethod() of the b39 class gets invoked. If the methods were not static the b40 amethod would override the super method because the variable would not know the correct method to call until it looked at what it was currently holding (that is why it is called late binding).
As for the exception:
From the JLS


A method that overrides or hides another method (�8.4.6), including methods that implement abstract methods defined in interfaces, may not be declared to throw more checked exceptions than the overridden or hidden method.



[This message has been edited by Cindy Glass (edited May 08, 2001).]
 
shadow liu
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Cindy.
Your JLS for exception clear my confuse.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic