• Post Reply Bookmark Topic Watch Topic
  • New Topic

Static methods overridden question  RSS feed

 
Tiberius Marius
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I m confused with something i read in Intro to Java Programming and i can't seem to arrive at a conclusion myself . The text is :

"Like an instance method, a static method can be inherited. However, a static method
cannot be overridden. If a static method defined in the superclass is redefined in a
subclass, the method defined in the superclass is hidden. The hidden static methods
can be invoked using the syntax SuperClassName.staticMethodName . "

Ok , a static superclass cannot be overridden and if one is defined in the subclass the one in the superclass will be hidden .But wait , doesn't that mean that the class can't be accessed using super.static_method like all the rest of the overridden methods ?

I ve verified this :



My question is what's the difference if you can still access it using super , how can you make the difference between it being overridden or not ?
 
Chris Barrett
Bartender
Posts: 321
24
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tiberius,

It's all about polymorphism.

For a detailed explanation, check out: http://www.coderanch.com/how-to/java/OverridingVsHiding

(Funny enough, this came up last week in a LinkedIn forum as someone was asked this during a job interview).

Cheers!
Chris
 
Arthur Vinicius Rebelo
Ranch Hand
Posts: 30
1
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tiberius Marius,

To start, static method can not be overridden.

What the quote from the book your are reading is trying to state , I guess, is the following:

As you probably know, to invoke the class method x you can either A.x() or B.x().
That is so because the class B inherits all the accessible members from A (i.e. class method x).
But what happens if you define a class method within B with the same signature?

A.x() stays the same, but B.x() now invokes another method. It invokes the one defined in B.
Don't forget, overriding isn't occurring here. You can prove yourself that by:

And of course when you try to invoke x with an unqualified method invocation expression within B, it will invoke x from B.


And as for your both questions I couldn't understand very well, if you rewrite them that would be nice. Good luck.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tiberius Marius wrote:I ve verified this :

What exactly have you verified? What does that program print out? Does it compile?

My question is what's the difference if you can still access it using super , how can you make the difference between it being overridden or not ?

For something like your scenario, you can't; but it's probably worth pointing out that invoking static methods via object instances is almost always the wrong way to do it. Indeed, some of us wonder why Java ever allowed it. I suppose it allows a form of "static polymorphism" (oxymoron?), but I personally find it extremely confusing when I see it in code.

The fact is that static methods should generally be avoided unless:
a. You have no choice.
b. The method is completely self-contained - and in that case (and if it's useful) you might want to think about putting it in a utility class.

HIH

Winston
 
Tiberius Marius
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris the link was very helpful and i ended up bookmarking it for my OCA study .

I m sorry my questions were not very clear , i read that static methods cannot be overridden but then i noticed they appear to behave exactly like overridden methods . For one just like super can be used to call an overridden method in the superclass it could be used here as well.So my questions revolved around "if it acts as a duck how come it's not a duck / what's the actual difference " ? Now i understand that the difference is that a static method does not have run time polymorphism aka looking at the classes in my example test5 obj1 = new cxcxcx(); obj1.m1(); will always look at the declared type (in case of static methods) and never at the actual type , meaning in this case that the m1() from the superclass(test5) will be invoked as opposed to a the m1() from the subclass(cxcxcx) if the method m1 would have been an instance method (run time polymorphism).

You are ofc right Winston , static methods should not be called via object instances.

Thanks everyone for clearing the confusion on this subject.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tiberius Marius wrote:For one just like super can be used to call an overridden method in the superclass it could be used here as well.So my questions revolved around "if it acts as a duck how come it's not a duck / what's the actual difference " ? Now i understand that the difference is that a static method does not have run time polymorphism aka looking at the classes in my example test5 obj1 = new cxcxcx(); obj1.m1(); will always look at the declared type (in case of static methods) and never at the actual type...

And just to clarify (I hope ) I suspect that the compiler can actually make a determination as to what 'super' means in the context it's used; so there's no actual runtime determination involved.

Good question though.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!