• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

static

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please tell me final answer in yes or no of tis question
that "Static method can be overiden"
Although i have seen that if i create an object using reference of superclass superclass method is called.
Anurudh
 
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
YES, static methods can be overridden.
You cannot override a static method to be non-static.
A static method does not have this.
A static method cavn only call static methods of its class
A static method can only access static data of its class.
Regards
Danish Shaukat
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Example:

Try running this example for yourself. The result is:
Static1:method1
Static2:method1
So we can see that class Static2 has successfully overridden method1 of Static1.
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Danish, Thomas ...
Thomas, your example does compile and run but technically a static method cannot be overridden ... it's actually hidden.


If a class declares a static method, then the declaration of that method is said to hide any and all methods with the same signature in the superclasses and superinterfaces of the class that would otherwise be accessible to code in the class. A compile-time error occurs if a static method hides an instance method.
JLS�8.4.6.2


It's a fine point, not sure wether it would come into play in the Exam but just in case
Also, you can't reference the hidden method from an instance of the subclass.

------------------
Jane
The cure for boredom is curiosity.
There is no cure for curiosity.
-- Dorothy Parker
[This message has been edited by Jane Griscti (edited November 30, 2000).]
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting, but it isn't clear, at least to me, what the difference is between hidden and overridden. For example:

Will run method1 of Static1 exactly as one would think. So what has been hidden and from whom?
 
Enthuware Software Support
Posts: 4910
60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basically, the concept is of polymorphism. Overriding is just a way to achieve it.
Base b = new SubClass();
b.m1(); //here subclass's m1() should be called as the actual object is of class SubClass.
Now, static methods and fields do not take part in polymorphism.
ie. Base.sm1(); will call Base's static method and SubClass.sm1() will call SubClass's static method (as you would expect). But the interesting point is b.sm1() will call Base's method even though the object is of class SubClass. That's why it is said that static methods are not overidden but are hidden.
Hidden means you can go behind the curtain and can access the hidden thing. Ex.
SubClass s = new SubClass();
s.sm1(); // will call SubClass's static method.
s.m1(); // will call SubClass's instance method.
( (Base) s ).sm1(); // will call base's static method.
( (Base) s ).m1(); // will STILL call Subclass's instance method. There is no going behind the curtains!
HTH,
Paul.

------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus
 
anurudh
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your help it helped to make me build my concept
Anurudh
 
He loves you so much! And I'm baking the cake! I'm going to put this tiny ad in the cake:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic