The output what you got is correct. "super.k" will print the value of "k" present in the Super class.
When not being initialized, the instance variable is set to its default value. "k" being an int variable, it gets its default value 0 in Class A (superclass) and thats what gets printed.
when you call sub.showij() method, you are calling the showij() method via the subclass reference as the subclass very well inherits the superclass members. But still, inside the showij() method, when you print the value of the variables i, j, k it will tend to access the variables of superclass only! Thats why you get 0.
The same you are trying to explicitly achieve by calling "super.k" in the showk() method in subclass, where the value of "k" is printed as 10 and "super.k" is as said above 0.
for the sub class when you assign the values for variables in main, the k in Bhides the k in A (as explained previously).
method showij() can access the variables declared in the same class as it is declared in, and not its sub-class variables.
in general, a method in a class has access to inherited variables and methods. if a class declares variables that have same name as variables in its super-class then those super-class variables are hidden. however, on calling inherited methods, the methods have access to variables that are only visible to them.
to put it crudely, 1. a sub-class is more powerful than a super-class, and not the other way round. 2. only which is visible* can be accessed. (*visible means w.r.t accessibility, of the member)
Joined: Jul 04, 2007
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.
Joined: Jul 02, 2007
oK jenings,Muthu.But the variable 'k' for subclass is declared before it is called.When the instance of subclass calls the method in the super it must display the values of subclass.It shows all the values for the variables which are not in the super.The variables which are in the super when called by subclass shows "0".Till this one is not clear
Shyam Prasad Murarka
Joined: May 02, 2005
Dear Ganesh Kumar, Taking your code from this point:
Now class A has these variables: i, j, k class B has this variable: k
Lets break down the program into simple steps:
When you call sub.i=82, the compiler checks for a variable i in class B, it does not find any! So it travels one step higher in inheritance tree to class A and finds the variable i, so it assigns the value 82 to that variable.
Same process takes place as above for the statement sub.j=32
Now when it encounters sub.k =10, the compiler finds k in class B itself so value of k is assigned there. REMEMBER THIS STEP!!!
When it reached sub.showij(), the compiler checks for this method, does not find any, so it proceeds to class A. It finds the method there.
Now pay close attention, this is the important part. The showij() method USES variables found in class A, and does NOT refer to variables found in the subclass! Now, if you see the third step again, you will notice that the compiler assigned the value of 10 to variable k of class B and NOT class A. So, by default, the value of k in class A is initialized to 0 and that is what you get in your output.
But, if you call sub.showk() method, then the compiler uses the k variable of class B.