aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes method overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "method overriding" Watch "method overriding" New topic
Author

method overriding

geeta rai
Ranch Hand

Joined: Sep 18, 2003
Posts: 48
Can static methods be overridden by static methods ONLY?
Rolf W. Rasmussen
Author
Greenhorn

Joined: Nov 04, 2003
Posts: 5
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.


"Plurality should not be posited without necessity."<br /> William of Ockham
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
But we can overload a static method in order to adapt with the different usage...


Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
Gurukeerthi Gurunathan
Greenhorn

Joined: Jun 10, 2003
Posts: 10
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

Joined: Jun 10, 2003
Posts: 10
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

Joined: Aug 25, 2003
Posts: 504
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

Joined: Apr 21, 2002
Posts: 48
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

Joined: Nov 04, 2003
Posts: 5
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

Joined: Aug 25, 2003
Posts: 504
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

Joined: Apr 21, 2002
Posts: 48
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

Joined: Aug 25, 2003
Posts: 504
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

Joined: Sep 18, 2003
Posts: 48
Thanks everyone for clarifying the doubt.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: method overriding