Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

method overriding

 
geeta rai
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can static methods be overridden by static methods ONLY?
 
Rolf W. Rasmussen
Author
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you declare a static method in a subclass that has the same method signature as a method in a superclass, then you are hiding the superclass method from the subclass. You are not overriding it.
Overriding methods implies that you will have dynamic method lookup (facilitating polymorphic behavior), which is not the case for static method.
So to answer your question: Static methods can never be overridden.
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But we can overload a static method in order to adapt with the different usage...
 
Gurukeerthi Gurunathan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Technically static methods can be overridden. My understanding is that overriding facilitates two functionalities and I don't know if we call a method to be overridden if and only if these two clauses are satisfied.
1. dynamic method lookup
In this case, static methods will not use the dynamic method lookup. We can say,
Superclass sc = new Childclass();
sc.overRiddenMethod();
where overRiddenMethod() is a static method in both super and child classes, and jre will execute the super class' static method instead, i.e. not performing the dynamic lookup.
2. providing a new functionality for the child class while hiding the superclass functionality.
If you say,
ChildClass cc = new Childclass();
cc.overRiddenMethod();
( ideally we shud be doing ChildClass.overRiddenMethod() as it's a static method, but for simplicity I am using this snippet)
In this case, the overriddenmethod of the child class is called.
-Guru.
 
Gurukeerthi Gurunathan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another observation after reading Geeta's posting. A static method CANNOT be overridden by a non-static method (aka instance method). An instance method of a superclass cannot be hidden by a static method of the subclass.
-Guru.
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am I allowed to introduce some confusion into such a clear environment?

Well, as long as it helps conceive the concept...
 
Amir Kamran
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello !
There is no confusion at all dear.
when you use no access modifier, only the classes with the same package can use the methods.
you are using two different packages, so the subclass not inheret the static doTest() method of superclass
 
Rolf W. Rasmussen
Author
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To clarify further:
The code

produces a compile-time error, because the code tries to override a static method. This behavior is defined by section 8.4.6.1 of the Java Language Specification.
The code

will compile successfully.
Neither of the code fragments successfully overrides anything. The last code fragment illustrates Hiding by Class Methods as described by section 8.4.6.2 of the Java Language Specification.
Overriding and hiding are closely related, since they both prevent the method from the super class from being inherited to the subclass, but technically it is incorrect to mix these terms.
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Amir Kamran:
hello !
There is no confusion at all dear.


OK, OK, buddy, I tried my best...
When classes belong to separate packages, non-trivial things sometimes happen. I just wanted to show one of the cases. BTW, in the same example,

doesn't compile.
 
Amir Kamran
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm...
SuperClass bc=new BaseClass();
this will not compile because of the same reason of accessibility ... we have to provide a public or protected constructor to instantiate a class in different packages.
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Amir Kamran:
hmm...
SuperClass bc=new BaseClass();
this will not compile because of the same reason of accessibility ... we have to provide a public or protected constructor to instantiate a class in different packages.

In fact, what I meant was this:

It doesn't have to do with constructors though because public default constructors were supplied by the compiler for both classes. Static doTest() is bound to SuperClass at compile time, but it's not visible outside of its package.
 
geeta rai
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone for clarifying the doubt.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic