• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Please solve my problem

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

When i execute the following code the value of "k" when called by the child instance is "0" (i have assigned "10" to it) .What is the reason behind it.I have used that variable in the parent class too

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

Variable "k" is once again declared in class B. By doing this you would be hiding the variable "k" present in super class A.

The reason you are getting zero, is because that value refers to variable "k" present in class A ,as showij() method is present in class A.

However in class B showK() method prints 10, because the variable "k" present in class A is hidden, as class B variable "k" overides it.

Modify the showK() method, you will get the exact picture.


Regards
Atul
 
Ganesh Kumar
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No prabhu i didnt get the output even after changing it to as you said.Even now i am getting super.k=0. What is the reason???

 
Ganesh Kumar
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Super values
Value of i and j:10 106 90
Sub values
Value of i and j:82 23 0
Value of k:10 super 0

This is what the output i am getting
 
Ganesh Kumar
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have not got cleared with the doubt? Still the problem is their
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ganesh Kumar

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.

HtH.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

you are getting the correct output.

for the sub class when you assign the values for variables in main, the k in B hides 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)
 
Michael Jennings
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


 
Ganesh Kumar
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


  • [ July 09, 2007: Message edited by: Shyam Prasad Murarka ]
     
    Ganesh Kumar
    Ranch Hand
    Posts: 113
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi shyam prasad muraka thanks a lot friend.Its ver good explanation.Thanks for all!!!

    Muraka thanks a lot again
     
    Shyam Prasad Murarka
    Ranch Hand
    Posts: 209
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Dear Ganesh Kumar,
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic