hello,
I'll try to explain to you:
First,a little doze of theory: when you override a method in a subclass, and you create an instance of that subclass, at runtime always the implementation from that subclass will run.
Now, in your example, you have the call() method in Parent class that is overriden in the Child class. That means that when creating Child objects, always call() from Child will run.
Then, theory also sais that the first line in a constructor (even if you don't explicitly write it) , is the call to super() (calls the constructor from the parent class). Ok .. there is an entire set of rules about calling no-args or with-args constructors (cap 2 from K-B book explains in detail ... entire book is excelent).
So, when you write new Child(), the following things will happen:
- the constructor of the Parent class will run.
- there is a call to call() method. but .. as I wrote you before, at runtime, the call() method from Child class will run, not the call() method from Parent class, because you are creating an instance of the Child class. At this stage, the non static attributes from Child class are not yet initialised (b is not 16 yet), but they receive the default value (0 for int)
- now, the constructor from the Parent class is finished running, and you can set the values for the attributes in the Child class, but the "b" variable is already given an explicit value, so at this point b=16
- then another call to call() method is made, and, again, the method from Child class will run
- now, the value of b=16, because you have explicitly set it so.
I hope you understand now.