aspose file tools*
The moose likes Beginning Java and the fly likes Why this behaves like super ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why this behaves like super ?" Watch "Why this behaves like super ?" New topic
Author

Why this behaves like super ?

Juan Manuel Alberto de los Santos
Ranch Hand

Joined: Jun 26, 2008
Posts: 48

[ July 29, 2008: Message edited by: Juan Manuel Alberto de los Santos ]
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Sep 03, 2004
Posts: 127
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.


J2EE Architect/Developer
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36501
    
  16
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

Joined: Aug 10, 2005
Posts: 685
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

Joined: Jun 06, 2007
Posts: 2712
    
    5

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


SCJP, SCWCD.
|Asking Good Questions|
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 685
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

Joined: Jul 08, 2003
Posts: 24166
    
  30

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.


[Jess in Action][AskingGoodQuestions]
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2712
    
    5

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why this behaves like super ?
 
Similar Threads
Overriding
Regarding Overriding
constructor calling super
Mock exam inheritance/varargs question
constructor