Win a copy of Microservices Testing (Live Project) this week in the Spring 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Static methods

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know that static methods cannot be overridden by non-static methods. And also, non-static methods cannot be overridden by static methods.
But, can static methods be overridden by static methods?
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
static methods cannot be overridden but they can be hidden, so u can have a static method in a child class with the same signature that will hide the static method in the parent class from classes that sub class the child ...though the static method in parent can still be ref'd as parent.staticMethod()
manav
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since static methods belong to the class( and not the instance, ) they don't participate in polymorphism. Hence the term 'overriding' should not be applied to static methods.
When a subclass redefines the static method defined in the superclass, the method in the subclass hides the method in the superclass.
HTH
------------------
Ajith Kallambella M.
Sun Certified Programmer for the Java�2 Platform.
IBM Certified Developer - XML and Related Technologies, V1.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can not override private, final, or static methods. You can not have a method with the same signature in a derived class as a final or static method in a base class. You can have a matching signature for a private method but it does not actually override the parent method. Overriding implies that polymorphism is available and in this case it is not.

------------------
Moderator of the Programmer Certification Forums
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Static method -->
In case of overloading:
Methods can be static or non-static. Since the methods are independent, it doesn�t matter. But if two methods have the same signature, declaring one as static and another as non-static does not provide a valid overload. It�s a compile time error.
In case of Overriding :
static methods don�t participate in overriding, since they are resolved at compile time based on the type of reference variable. A static method in a sub-class can�t use �super� (for the same reason that it can�t use �this� for)
Remember that a static method can�t be overridden to be non-static and a non-static method can�t be overridden to be static. In other words, a static method and a non-static method cannot have the same name and signature.
I think ..that helps.
Thanks.
<marquee>Ratul Banerjee </marquee>
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
to add to this, final methods cant be overridden at all.
i would say that we can override static methods in base class (by static or non-static) but they dont demonstrate polymorphism.
 
Ranch Hand
Posts: 400
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just doing round-table....!!
consider this code :
class A {
private static void staticMethod() {}
private final void finalMethod() {}
public void access() {
staticMethod();
finalMethod();
}
}
public class MyClass1 extends A{
void staticMethod() {} //will this compile ??
void finalMethod() {} //will this compile ??
public void access() {
super.access();
}
public static void main(String[] args) {
MyClass1 a = new MyClass1();
a.access();
}
}
The above code will compiled without error, don't be confused with the word static or final in class A, just pay attention on word private.
Since both method is declared private, it means it is not accessible outside the class in which it is defined, and thus a subclass can give it's own definition of methods which also happen to have the same signature as the methods in it's superclass.
but if the superclass method does not declared as private, then :
1. static method, can only be shadowed(not ovveride)with static method.
2. final method, can not be ovveride.
hope that helps
stevie
 
reply
    Bookmark Topic Watch Topic
  • New Topic