• 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

Doubt regaeding static for variables in inheritance

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

the output for this is:40



the output for this is:30 ..


when we used the static for 2nd program the value in "value" is 30 unlike the 1st...
is it that for static members the derived class maintains a separate copy of the variable from that of the parent class(where the value of "value " is altered)

 
Sheriff
Posts: 9689
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in the first case call to addValue is polymorphic inside the constructor of Base. This is why the answer is 40. In the second example, the call to addValue is not polymorphic as addValue is static. So it is not overridden in the Derived class...
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We cannot override the Static methods, static methods will be called based on the object reference type not on the object type.
So in the second case the statment b.getValue() will execute the object reference(Base class) type's method not the object(Derived class) type's method. i.e. It will execute the Super class getValue()
 
saipavan vallabhaneni
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks ankit and swamy,


sorry i am unable to understand what your trying to explian
can you please elaborate on the order of execution of the 2 programs n how the "value" is getting changed

 
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First we will take this code:




to understand this we need to know that run time polymorphism means that, the function which we call at runtime is decided by its type of object. Like if class A and class B(class B is subclass of class A) both have method named getValue() then even you use the reference of super class the method will be called of the class of which object is being used.

for eg:




The same thing is happening in the code snippet you gave before. When the JVM sees this it gets to the constructor of derived class and then to its super class without completing the derived class constructor. Now when the addValue function is called from the super class constructor then addValue of derived class is called as the object belongs to this class. Same happens when the control comes back to the derived class function call. and hence the function names 'addValue()' is called twice. So the output is 40.


For the later snippet of code which is giving output 30 you need to know that static functions are not overridden. Try again to understand it. I am sure that you will get its logic too.
 
saipavan vallabhaneni
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Himanshu,
thanks a lot for the explanation...i have now understood the logic...i sat for about an hour in the morning trying to figure it out but couldn't..thanks for your explanation

thanks everyone for helping me out
 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi saipavan

Please mention the source of the code.

This link from JLS will help clear your doubt link
 
saipavan vallabhaneni
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Harvinder,
i had gone through the link.. source of the program wasSource of the program
But now i have got another doubt...

in scjp5 by K & B page 130 its given that we cannot invoke the method instances or access any static variables untill after the super constructor runs.
so my doubt is whether the super constructor has to complete or it has to be just run and there after we can access the instance methodsif we look at the 1st program(one whose output is 40) the overloaded version of the method is called in the base class constructor before the constructor actually completes...so is it that its just enough if super constructor runs without being completed as in above case

 
Ankit Garg
Sheriff
Posts: 9689
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
saipavan the super constructor just needs to be invoked and not completed. In the following program we are trying to access instance method and field before the super constructor call

 
saipavan vallabhaneni
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks ANKIT
 
reply
    Bookmark Topic Watch Topic
  • New Topic