FacebookTwitter
Hatrack River Forum   
my profile login | search | faq | forum home

  next oldest topic   next newest topic
» Hatrack River Forum » Active Forums » Books, Films, Food and Culture » Java Question:

   
Author Topic: Java Question:
Phanto
Member
Member # 5897

 - posted      Profile for Phanto           Edit/Delete Post 
quote:


import java.awt.*;

public class Point3D extends Point {
public int z;

public Point3D(int x, int y, int z) {
super(x,y);
this.z = z;
}
}

this is the start of a program I'm working on that adds a third variable to the Point class. I'm not quite sure why it is neccesary to use the super() method. Aren't you making a constructer method without it? And what about the "this" part?

*is confused* [Frown]

Posts: 3060 | Registered: Nov 2003  |  IP: Logged | Report this post to a Moderator
Phanto
Member
Member # 5897

 - posted      Profile for Phanto           Edit/Delete Post 
I think I've got it: the super() method sets the x,y variables to what is defined in the superclass; the this thing refers to the object being made.
Posts: 3060 | Registered: Nov 2003  |  IP: Logged | Report this post to a Moderator
King of Men
Member
Member # 6684

 - posted      Profile for King of Men   Email King of Men         Edit/Delete Post 
It is not strictly necessary to have 'super' in all cases. If Point had a default constructor, say setting both x and y to zero, then you could do without it. But since it doesn't (if I remember rightly) the poor compiler needs to know how it should initialise the members of the superclass.

Now, this may appear rather obvious to you; in fact, for this example, you don't really care what the compiler does about the superclass, you can set its members yourself. But that's because the Point class is deceptively simple and has public members, and compilers aren't very bright anyway. In a more complex case, your derived class might not have access to the members of the superclass, members which absolutely need to be set. Hence the call to super.

About 'this', that's your own fault for making the code difficult. You have a local variable, the 'z' passed to the constructor, masking (that is, it has the same name as) a member variable, the 'z' belonging to the Point class. This is a bad idea! In fact, if you look back a couple of pages, you'll find a thread of mine titled 'Good news, bad news' where I give an example of the possible consequences of that mistake. In my case it was very un-pretty.

But getting back to 'this' : The keyword dis-ambiguates the 'z' for the compiler. "this.z" means "the z variable belonging to the particular object that I am now working with", that is, the one you are constructing. Just plain 'z', on the other hand, means "whatever variable you can find called z", and this will find the local variable first. Now, if you had

code:
 public Point3D(int x, int y, int setZToThis) {
super(x,y);
z = setZToThis;
}

you wouldn't need the keyword, since 'z' is now un-ambiguous again - it can only refer to the member variable.

By the way, here's why masking variables is a bad idea; suppose you had said

z = someComplicatedCalculation();

Then the compiler would assign the result of someComplicatedCalculation() to the local z, which disappears as soon as the method is done! You would then be extremely confused when you later tried to use the member variable z, and found it hadn't been initialised.

Posts: 10645 | Registered: Jul 2004  |  IP: Logged | Report this post to a Moderator
King of Men
Member
Member # 6684

 - posted      Profile for King of Men   Email King of Men         Edit/Delete Post 
Gah, don't figure things out faster than I explain them!
Posts: 10645 | Registered: Jul 2004  |  IP: Logged | Report this post to a Moderator
Swampjedi
Member
Member # 7374

 - posted      Profile for Swampjedi   Email Swampjedi         Edit/Delete Post 
Excellent explanation, KoM.
Posts: 1069 | Registered: Feb 2005  |  IP: Logged | Report this post to a Moderator
Phanto
Member
Member # 5897

 - posted      Profile for Phanto           Edit/Delete Post 
But now you know that you know, and that, my dear friend, is the best feeling in the world.

*blink blink*

[Wink]

Posts: 3060 | Registered: Nov 2003  |  IP: Logged | Report this post to a Moderator
Phanto
Member
Member # 5897

 - posted      Profile for Phanto           Edit/Delete Post 
oh, and thanks. ^^
Posts: 3060 | Registered: Nov 2003  |  IP: Logged | Report this post to a Moderator
   

   Close Topic   Feature Topic   Move Topic   Delete Topic next oldest topic   next newest topic
 - Printer-friendly view of this topic
Hop To:


Contact Us | Hatrack River Home Page

Copyright © 2008 Hatrack River Enterprises Inc. All rights reserved.
Reproduction in whole or in part without permission is prohibited.


Powered by Infopop Corporation
UBB.classic™ 6.7.2