Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Static methods - overriding

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone.

On some lecture notes of my teacher, I read that static methods cannot be overridden.

In the same notes, I found these lines of code (the image you can find as attachment).

I don't understand.
Firstable, he explained that static methods cannot be overridden.
Then, he shows a working example where he ovverrode the static methods.

Is anyone capable to explain?
java-1.PNG
[Thumbnail for java-1.PNG]
 
Saloon Keeper
Posts: 11882
253
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method wasn't overriden. It was hidden, as the comments in the code say.

The difference is subtle: When you override a method, you take its inherited method declaration and you provide it with a new method body. When you have an instance of a class with an overridden method, it still only has one declared method. When you hide a method, you declare a completely separate method that just happens to have the same name. You now have two different methods.

The code also shows when the difference matters: If you call a hidden method through a reference of the base class, the hidden method definition in the base class will be executed. On the other hand, if you call an overridden method through a reference of the base class, the overridden method definition in the derived class will be executed:

Please keep in mind that this is just an explanation of the difference. In the real world, you would never call a static method through an object reference. Always call static methods through a type reference:
 
Sheriff
Posts: 15519
263
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another thing to note about static methods is that they're not polymorphic. That is, they are resolved at compile time rather than runtime.

If you have non-static methods like this:

Even though the reference used is declared as A, because the instance method f() is polymorphic, the actual method called is based on the runtime object's type, which is class B. This occurs through a mechanism called "dynamic binding."

However, if you have static methods as shown below, resolution of the call occurs at compile time and is based on the declared type, A. This mechanism is called "static binding."
 
Marshal
Posts: 68909
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Claud Mann wrote:. . . static methods cannot be overridden.

That's quite correct.

. . . these lines of code (the image you can find as attachment). . .

Please avoid such images. As well as what that link says, such images disappear from the reply window, so i can't even readily see it.

. . . he ovverrode . . .

No, he correctly said he isn't overriding those methods, but hiding them. This is what overriding means: the method used at runtime is found from the runtime type of the object, and that applies only to instance methods:-Try that code; you will get ??? printed.
In the last line of the code you showed, the reference is cast, and the method is called on the new declared type of that reference. You can get dangerous confusion if you try hiding static methods and thinking they are overridden. You can have a different method executed from what you thought. That is why it is considered bad practice to call static methods on an object reference. A static member is considered to belong to the class, not a particular object, and should therefore always be called on the class name. No risk of confusion now. I presume you were told that, too.

I did write this post before Stephan and forgot to push “submit”.
 
Campbell Ritchie
Marshal
Posts: 68909
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . a mechanism called "dynamic binding." . . .

I have also heard people say, “late binding,” and, “runtime binding.” Which of the three terms do you think is best?
 
Junilu Lacar
Sheriff
Posts: 15519
263
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I have also heard people say, “late binding,” and, “runtime binding.” Which of the three terms do you think is best?


I have no preference either way although the more common ones I encounter are early/late and static/dynamic binding. They're synonymous to me.
 
Stephan van Hulst
Saloon Keeper
Posts: 11882
253
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Another thing to note about static methods is that they're not polymorphic. That is, they are resolved at compile time rather than runtime.


While this is true, it's not to imply that non-static methods don't also use a form of static binding: method overload resolution is performed on the formal types of the method arguments.
 
Campbell Ritchie
Marshal
Posts: 68909
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wouuld be happy with all three terms though maybe runtime binding would be my favourite.
 
Claud Mann
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:

[/code]



Hi everyone, thanks for your answers.
Stephan I have a question for you. I understood that hiding and overriding a method are two different concepts, but I didn't understand how this hiding process works.

Why if I write :

the result will be linked to the method of the superclass?
I try to answer, tell me if this sounds right:
The reason is because what matters is the class to which obj belongs, and not the type of the object instantiated.
Why? Because the method is a static method, so the Dynamic Method Lookup doesn't intervene.
 
Junilu Lacar
Sheriff
Posts: 15519
263
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The reason is because what matters is the class to which obj belongs, and not the type of the object instantiated.
Why? Because the method is a static method, so the Dynamic Method Lookup doesn't intervene.


You're on the right track.

Start with this: Because demonstrateHiding() is a static method...

Then, the method call is resolved through early/static binding...

and finally, which means that the declared type, Base, is used instead of the actual type, Derived
 
Claud Mann
Ranch Hand
Posts: 31
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much!!
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does Java allows to override a static method which will not work ?

Is it possible to stop them in the compilation time itself?

Is there is any cases where static method overridden is going to work in java?
 
Stephan van Hulst
Saloon Keeper
Posts: 11882
253
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Johnny Joseph wrote:Why does Java allows to override a static method which will not work ?

Is it possible to stop them in the compilation time itself?


I'm not sure what you're asking here. Java doesn't allow you to override static methods.

Is there is any cases where static method overridden is going to work in java?


Again, not sure what you're asking. Static methods can't be overridden.
 
Johnny Joseph
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree static methods can't be overridden...However in the other cases the difference comes only during runtime. Why java couldn't catch in during compilation itself?
 
Campbell Ritchie
Marshal
Posts: 68909
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Johnny Joseph wrote:. . . couldn't catch in during compilation itself?

Catch what? There is no syntax error in such code. It obeys the behaviour specified for it in the Java® Language Specification exactly. If the behaviour is different to what I expected, that means I have programmed it wrongly.
 
Stephan van Hulst
Saloon Keeper
Posts: 11882
253
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you mean, why does Java allow you to hide static methods if calling them executes the superclass' version? That's only the case in the example I posted. Had I written:
then calling obj.demonstrateHiding() would have printed "Hiding method in derived class."


The confusion is caused because Java allows you to call static methods on object references. This was a mistake. Java should only have allowed calls like:
and:
instead of:


If your question is, why does allow Java to call static methods on object references, then the answer is that the designers didn't think this would cause any problems, and after they added it in the very first version of the language, they couldn't take it out anymore.
 
Johnny Joseph
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan..got it.....Thanks..is there is any place where i can see these kinds of designer's issue where they couldn't take it anymore....
 
Do the next thing next. That’s a pretty good rule. Read the tiny ad, that’s a pretty good rule, too.
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic