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.