This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
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
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.
Joined: Jun 10, 2003
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.
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
Joined: Nov 04, 2003
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 18.104.22.168 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 22.214.171.124 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.
Joined: Aug 25, 2003
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,
Joined: Apr 21, 2002
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.
Joined: Aug 25, 2003
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.