This week's book giveaways are in the AI and JavaScript forums.
We're giving away four copies each of GANs in Action and WebAssembly in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript 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
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

static methods can't be overidden why

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am thinking static methods are loaded at the time of compilation so static methods can't overidden at runtime is it right?plz clarify my dout....
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I overload my static methods all the time, so they can be overridden...
 
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static methods can't be overriden. Just try it. You will fail.
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by David Ulicny:
Static methods can't be overriden. Just try it. You will fail.




I tried it here:
I succeeded !!


 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
static methods are linked at compile time (like variables) so if you override them they get hidden.

so if you have two classes:


and then in the following code you do something like:


the output will be:

because as far as the compiler is concerned champion is an animal not a horse.
[ August 16, 2005: Message edited by: Struan Kerr ]
 
David Ulicny
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the output:


So, you just call constructor Test() which calls constructor Base() and inside both constructors you just call both methods staticMethod(). I cannot see overriden static method.
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static methods are *not* overridden. Semantic nitpickiness follows:

�8.4.8.2, JSL
If a class declares a static method m, then the declaration m is said to hide any method m', where the signature of m is a subsignature (�8.4.2) of the signature of m', in the superclasses and superinterfaces of the class that would otherwise be accessible to code in the class. A compile-time error occurs if a static method hides an instance method.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static methods can only be overridden by Staic methods, they cannot be overridden by instance methods in the child class.
hope this clears up the doubt!!!
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Swapna", please read our JavaRanch Naming Policy, and change your displayed name to conform. We require two names in the format <first name><space> <family name>, preferably your real name.
Thanks
-Barry
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am still not able to understand it completely. can anyone explain that in a little more detail ?

Thanks in advance.
- Dharmesh
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I got confused I need to review the concept again.
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
static methods are linked a compile time (like variables) so if you override them the get hidden.

if u take this code u can call the static method with out create a instance static members are direclty related to the class



the output was
staticMethod() in Subclass Test.
15
staticMethod() in Class Base.
5

So when u call the static method using the Child Class i.e DemoTest.staticMethod() it will hide the base class implementation and it executes the child class implementation.

again if u call the method using the DemoBase.staticMethod() it will execute the base class implementation ...

so instance does not hava a role in this case ... So static methods can be override but it was linked to the compile time(static) so it will try to hide the implementation....

supoose take this scenario

the output was ..

staticMethod() in Class Base.
5
staticMethod() in Subclass Test.
15
staticMethod() in Class Base.
5

see when i was calling DemoTest1.staticMethod() it give me the output of base class since DemoTest1 child class of DemoBase was not overridding the staticMethod() so the implemention of the base class was not hidden..
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Create a child class object and assign it to Parent reference.

Now call any static method that has same signature in both the classes(parent & child), ur parent static method will be called since ur reference is of parent type.....

Even though ur object is of child type.... the binding happened during compile time and during compile time the compiler knows only about the type of reference(parent) not the object(child) to which it is refering to.That is why ur parent static method will be called.
 
I can't take it! You are too smart for me! Here is the tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!