• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Overriding static methods?

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've read that static methods can be overriden however, the superclass overriden static method goes hidden. What exactly is happening when you say "hidden"?

I tried below code and I don't see difference from overriding from normal method overriding.




Please provide concrete explantion if possible. I'm beginner and most of the concepts seem confusing. Thank you.
 
Marshal
Posts: 72905
330
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

milind k das wrote:I've read that static methods can be overriden . . .

Where? That source is mistaken; static methods are always hidden. Hiding a method or field means that the subclass version is called and it takes no notice of the superclass' version. A static member of a class should always be called by ClassName.memberName, never objectReferenceName.memberName, because they are not overridden and the version called depends on the declared type of the reference, not the runtime type of the object behind that reference. See our FAQ.
 
milind k das
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello. Thank you for response. I came across it in some online search. Unfortunately I couldn't get the source. But I understood your explanation in the link provided in FAQ.

Consider below program:



I see below output:

Ab Static Method.
Cd Static Method.
Ab Static Method.

For        
Ab cd = new Cd();
cd.dash(); , I debugged and here's the flow.

Here during execution: default super() of Cd class is executed first which means, the object of Cd class is executed first. Then dash() method in superclass is executed. So we see "Ab Static Method." first. Then it goes to Cd() dash() method. Its printing Cd Static Method.
And again it is going to Ab() to dash() method.

I couldn't understand this flow. If static method of reference type is considered, why is Cd class static method is being printed?

And if you replace Ab with Cd in line Ab cd = new Cd(); you will see below output:
Ab Static Method.
Cd Static Method.
Cd Static Method.

Here Ab static method is supposed to be hidden, correct? Why is it being executed?
 
Campbell Ritchie
Marshal
Posts: 72905
330
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

milind k das wrote:. . . the object of Cd class is executed first. Then dash() method in superclass is executed. . . .

No, you don't execute objects, but methods and constructors. The superclass' constructor is executed first, not that in Cd, and that fist constructor calls dash(); It is implicitly called as this.dash(); and this implicitly has the type Ab so the Ab method is called.
Then you descend to the subclass' constructor where dash(); is implicitly called on type Cd.
Then line 6 is reached which calls dash(); on the declared type of the reference.
 
Saloon Keeper
Posts: 12988
281
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The flow goes like this:

On line 5 you call the Cd() constructor. The Cd() constructor first calls the Ab() constructor before it continues with its own constructor body.
The Ab() constructor calls the Ab.dash() method, which prints "Ab Static Method".
Control returns to the Cd() constructor, and it calls Cd.dash(), which prints "Cd Static Method."
Control returns to the main() method, which calls Ab.dash() (because the cd variable is of type Ab). The code once again prints "Ab Static Method".

When you change the type of the variable from Ab to Cd, line 6 in the main method will call Cd.dash() instead of Ab.dash(), because now you call dash() on a variable of type Cd.

Hiding a member doesn't mean it will never get called. Hiding a member means you can't call the member using a reference of the subtype. But inside the Ab() constructor there is no reference of the subtype. There is only the Ab.dash() method.

Don't worry about this code too much. You are confusing yourself with code that never should have been written in the first place. Static methods aren't meant to be called on references, and you also shouldn't redeclare them in subclasses.
 
reply
    Bookmark Topic Watch Topic
  • New Topic