Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why this behaves like super ?

 
Juan Manuel Alberto de los Santos
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

[ July 29, 2008: Message edited by: Juan Manuel Alberto de los Santos ]
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Juan Manuel Alberto de los Santos:
System.out.print(this.i); //this shouldn't work but it does !


you can use superclass variable in subclass right?

this.i is same as i//this refers to the currently executing object
 
Henrique Ordine
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think seetharaman is right.
If the variable i had been private, then you wouldn't be able to reach it. And you wouldn't be able to reach it by prefixing with super either. Also if your variable had been overriden, then you'd have a difference between this.i and super.i.
 
Campbell Ritchie
Sheriff
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are quite right, Henrique Ordine, except that you shouldn't say "overridden." The variable is "hidden;" Joshua Bloch in Effective Java shows that you can get subtle errors from hidden class members, and warns against hiding. See this FAQ.
 
Norm Radder
Ranch Hand
Posts: 728
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This brings up a question I've had for a long time:
Why don't PC compilers give a symbol cross reference as part of the compile process. Then you could easily see the scope of the variable that you think is local really belongs to the super.

My background is mainframe assembly code.
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Norm, firstly please UseOneThreadPerQuestion.

Why don't PC compilers give a symbol cross reference as part of the compile process.


How will the compiler give such error if there is no symbol cross reference?

I mean if the compiler is able to resolve the scope, it wont give any error.Since the smallest possible scope is the block.
Also inheritance is part of OOP.


Hope this helps
 
Norm Radder
Ranch Hand
Posts: 728
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You miss my point about a symbol cross reference. It has nothing to do with compiler errors.
The cross reference listing for a symbol would show where the symbol was defined and where it was used. In the above discussions there was doubt about where a symbol was defined. A cross reference would show where the symbol was defined and remove the doubt.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Norm Radder:
A cross reference would show where the symbol was defined and remove the doubt.


Yes, indeed, although I don't know how many people these days would have the patience to go through it! Here's a bog entry in which I talk about another unpopular solution to this same kind of problem. Read the comments, where people bring up both syntax-coloring and compiler warnings as additional ways to address it.
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Norm I get you now, the only thing was I took it from the compiler's perspective

[Ernest:] I did show the patience to go through the blog, although there are already replies over there, I feel no doubt your solution works but again it makes code difficult to read.

In my view such scope problems can be solved by using more specific variable names. Like instead of saying simply size I would say sizeOfArray.
But again I know that this scheme also has limitations.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic