• Post Reply Bookmark Topic Watch Topic
  • New Topic

overriding static methods in subclass  RSS feed

 
Rhea Karnam
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
can someone help me make sense of this line (i am sure this is meaningful, only i can't understand what it means...)
"
Also, a subclass cannot override methods that are declared static in the superclass. In other words, a subclass cannot override a class method. A subclass can hide a static method in the superclass by declaring a static method in the subclass with the same signature as the static method in the superclass.
"
taken from: http://java.sun.com/docs/books/tutorial/java/javaOO/override.html
 
Ransika deSilva
Ranch Hand
Posts: 524
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
You have to remember a key feature about static methods, that is, they are NOT inherited. WHAT!!!, Yes, it means that a static method in a super class will not be passed to a sub class of it. So for example :

The Sub class doesn't have a method called aMethod().
Ok, so far so clear is it? move on then.
So, if you add a method like 'public static void aMethod()' to the class Sub, you are giving a BRAND NEW method to the Sub class. So now the class Super and the class Sub both have the same method with the same definition, but the point is that, this is NOT overriding, it's just having two different methods. So that's why it is said that you are hiding the method of the Super class by adding the same method in the Sub class.
Aha, so you get this didn't, so if you have any more problems, just post it. Have a nice day.....
 
Vijaypal Singh
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually you need to understand the difference b/w Hiding and Overriding.Hiding would in a laymans terms mean "Making inaccessible" and that is it Hiding a method makes it inaccessible to the subclass but when you actually override the method you can still access the superclass method using polmorphism.
And since Static stuff is releted to the class and not the object you cannot pass it along to the next lower level.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ransika ,

Ur explanational was very clear .
But just explain one thing why we declare static method I mean what is the use of it .
Is there any performance issue related to that ?

Thanks
 
Vijaypal Singh
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You declare a method static to use it without creating an object ie ClassName.MethodName().
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I already knew this . But is the only reason ?
 
Rhea Karnam
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thot i got it all correct, until i tried this out:

public class Base
{
public static int tryIt()
{
return 1;
}
}

public class Sub extends Base
{
public static void main(String args[])
{
System.out.println(Sub.tryIt());
}

--------------
and guess what??? it worked! I got the output as 1, and obviously no compilation errors. So, why is the static method not hidden? (Thats what i understood... that static methods are _not_ available for sub classes)...
 
Rhea Karnam
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok ...
I think i got the difference now...
I tried playing around a bit with some base and sub classes and here's what i inferred:
1. i dunno if the quoted stmt has anything do with the eligibility of a static method to be passed down to a sub class.
2. but, you can use the static method defined in the base class, in the sub class, with essentially the same results
3. when u "override" (or u think thats what u do) a static method, in the sub class, what you are essentially doing is hiding the Base.staticMethod's impl and giving an entire new implementation for the Sub.staticMethod() instead.
So, Base.staticMethod() is still very much different from Sub.staticMethod(), except for the fact that you can no longer say Sub.staticMethod() to call the Base's impl (by virtue of inheritence). That previlege is erased off because u now have your own method in its place.

I doubt if i make much sense here. But try writing a small pgm, and u will understand what i understood!

Thanks folks for the replies.
-Rhea
P.S. this reply is for ppl who had/have the same doubt that i had.
 
Richard Vagner
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You have to remember a key feature about static methods, that is, they are NOT inherited.


I do not think the above statement is correct(as far as Java is concerned).
On the contrary, static methods are inherited. That is why Rhea�s code works since the static method tryIt() is simply inherited from Base class.

Here is my 2 cents:

1. The reason we have method overriding is to have the ability to redefine new behavior of the super class method in the subclass which is invoked by the runtime object. This behavior is determined by polymorphism.

2. Static methods can not be overridden but can be shadowed (hidden) in the subclass. Once they are shadowed, they appear to be "overridden", but polymorphism will not apply to them. In that sense, it is not overriding.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's see how it all works out. Make class A with two methods, one static and one not. Make class B that extends A and the same two methods. Then make a method like this:

If you call this method with an instance of A, A's code runs for both methods. If you call it with an instance of B, it will run B's non-static method and A's static method. Why? Cause I told the compiler it's working with A and the compiler wired up the static method call to the class A.

I'm not sure it necessarily had to be this way. The language designers might have chosen to look for the static method on the actual class passed just like a non-static method.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stan James:
I'm not sure it necessarily had to be this way. The language designers might have chosen to look for the static method on the actual class passed just like a non-static method.
The problem comes when A's static method (static1) -- not defined in B -- calls another static method (static2) on A which is defined in B.We all agree that must print "A". But what about this?In reality, "A" is also printed (no polymorphism for static methods). But what if Sun had tried to work in polymorphism? In A.static1(), the reference from which it was executed -- an A pointing to a B -- is no longer available, and thus it cannot figure out that it should call B.static2().

Thus, you'd be left with sometimes getting polymorphism (if B had also defined static1()) and sometimes not. I think that confusion wouldn't be worth the minor gain. Besides, you can simulate it by defining a partner instance method that delegates to the static one. It's a kludge, but it works and is rarely needed.
[ January 05, 2005: Message edited by: David Harkness ]
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another detail is that a static method in a subclass that hides a static method in a superclass must follow the same compatibility rules concerning access modifiers, return types, and throws clauses as overriding methods. On the other hand, you can invoke a hidden superclass static method using super.methodName() or ((SuperClassName)subclassReference).methodName() as well as SuperClassName.methodName().

If the superclass static method is private, this isn't considered hiding or overridng, so these rules don't apply.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent point, David. Thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!