• 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
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

static overloading

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the following code


When I run TestStaticOverloadingChild class, it prints
parentm1
childm2

I know that static methods are decided at compile time. However would method m3 be not inherited in this case and should instance of TestStaticOverloadingChild not call m1 of TestStaticOverloadingChild class?

Thanks in Advance
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dave,
Static methods cannot be overridden. Only method m2 is overridden in the Child class.
When you run the code, you invoke m3() on the object of child but since its doesnt have that method, it will invoke the method in Parent(as it extends Parent)..so your invoking Parents m3() from which there's a call to m1(). m1() is static and belongs to the class. Invoking m3() of Parents executes m1() of Parent only. (Try overridding m3 in Child & and you can see the difference). But when invokation of m2 comes, it will execute Child's method as that method is overridden in the Child.

Try compiling and running the code by removing "static" from m1 in both class. you will see the change.
 
author
Posts: 23907
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I know that static methods are decided at compile time. However would method m3 be not inherited in this case and should instance of TestStaticOverloadingChild not call m1 of TestStaticOverloadingChild class?



Inherited doesn't mean that the child class gets a copy of the method, if it doesn't override. It just means that at runtime, the parent's version of the method will be called -- because it didn't override.

The parent version of the method, has already been compiled to called its version of that static method. It doesn't know that the child has its version of the static method.

[EDIT: And maybe the title should be renamed -- as this question is about calling static methods from an overridden (or non-overridden) methods. And not about overridding static methods.]

Henry
[ September 13, 2008: Message edited by: Henry Wong ]
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
static methods are NOT overridden. Hence, m1 () of the parent class is not overridden in the child class. Also, which overridden method to call is dependent on the object active during runtime.

Based on the above: "new TestStaticOverloadingChild()" refers to the child class's object and hence:
(1) the non-static method m2() which can be overridden is called from the child class.
(2) the static method m1() which cannot be overridden is called from the parent class.
 
Matiz Dave
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Inherited doesn't mean that the child class gets a copy of the method, if it doesn't override. It just means that at runtime, the parent's version of the method will be called -- because it didn't override.



I am calling method m3 from an instance of TestStaticOverloadingChild and thought that it would get an implict access to m3 as if the method belonged to TestStaticOverloadingChild. Does it mean that at runtime, jvm maintains a copy of each object up the heirerchy? Or only one object is created but the object has information about all the objects which this object has inherited from?
 
Henry Wong
author
Posts: 23907
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Matiz Dave:

Does it mean that at runtime, jvm maintains a copy of each object up the heirerchy? Or only one object is created but the object has information about all the objects which this object has inherited from?



For methods, neither description is completely correct. Method definitions are *not* part of the instance -- it is really expensive to make copies of every method for each instance.

Java does have the class definitions for all the classes up the heirarchy. And the instance does point to them. But there is no copy of each of the objects. It is one object with the correct jump tables to the correct class objects which have the methods.

Henry
 
Matiz Dave
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rekha, please check your private messages. You can see them by clicking My Private Messages.
 
There are 29 Knuts in one Sickle, and 17 Sickles make up a Galleon. 42 tiny ads in a knut:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic