• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inheriting variables

 
marilyn murphy
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Since this code will not compile if the line marked "line 1" is not commented out due to "i is already defined in Pri", why don't we get the same compile error due to the i Pri inherited from Base?
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Marilyn,
There are no errors because the 'i' in the class 'Pri' hides the 'i' in the class 'Base'. If you don't want this you can change the name of the variable to something else. Also I just modified your code. Have a look.

HIH
---------------------------------------------------------

[This message has been edited by chandrashekar munukutla (edited October 24, 2001).]
 
marilyn murphy
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So i in Pri hides i in Bases even though one 'i' is static and the other is not?
 
R K Singh
Ranch Hand
Posts: 5384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think yes.. as both have same identifier. yes but static var can be access by class name also and can be used in static methods.
CMIW
even static methods are not overridden but they are hide.

------------------
Regards
Ravish
 
Gurpreet Sachdeva
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Marilyn,
That is true that i in Pri will hide i in Base even if one is static and other is not. But you need to comment Line 1, as you already had one variable named i in the current scope. You can't declare two variable with the same name in the same scope.

------------------
Regards
Gurpreet Sachdeva
For Mock Exams, FAQ, Exam tips and some useful information about Bitshift operator, inner classes, garbage collection,etc please visit: http://www.go4java.lookscool.com
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic