hi
the output is correct.
the
k of A and the
k of B are initialised to 0 during the creation of an object of B, i.e. when you do
B sub = new B().
when you are initialising the variables of
sub class B in
main(), the
k of B is initialised and
not the
k of A.
using the reference B, when you call
showij() the value displayed is that of
k in A, and on call to
showk() the value displayed is that of
k in B and
k in super of B which is A.
in general, a sub-class inherits variables and methods of its super-class. however, the sub-class can declare variables with same name as that in super-class which then hide the super-class variables.
when an inherited method is called, the method by virtue of being declared in the super-class has access only to members of that class (so you see the output as it is).
think of the super-class and sub-class as an is-a relation, rather than as a parent-child relation.