# Unresolved and not visible with inheritance and objects

• (2 Pages)
• 1
• 2

## 24 Replies - 2519 Views - Last Post: 27 March 2013 - 03:49 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=316750&amp;s=bd04250af9e471754f30ed13f6dad3bf&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #16 jon.kiparsky

• Beginner

Reputation: 11022
• Posts: 18,805
• Joined: 19-March 11

## Re: Unresolved and not visible with inheritance and objects

Posted 26 March 2013 - 02:13 PM

Cube iceCube = new Cube(iceCube.getX(), iceCube.getY(), iceCube.getSL(), depth);

iceCube doesn't exist at this point - you're just declaring it now - so how can you be calling methods on it?

### #17 Flukeshot

• A little too OCD

Reputation: 417
• Posts: 1,030
• Joined: 14-November 12

## Re: Unresolved and not visible with inheritance and objects

Posted 26 March 2013 - 02:17 PM

Square mySquare = new Square(mySquare.getX(), mySquare.getY(), sideLength);

Tell me.. How do we know what mySquare.x is at this point?

In this scenario, the constructor asks you "How big is the new square going to be?" and you reply "make it as big as the new square is."

This simply cannot work. You need to be giving the new square values that already exist.

### #18 HGPolumbus

Reputation: 0
• Posts: 106
• Joined: 22-March 13

## Re: Unresolved and not visible with inheritance and objects

Posted 26 March 2013 - 02:19 PM

jon.kiparsky, on 26 March 2013 - 02:13 PM, said:

Cube iceCube = new Cube(iceCube.getX(), iceCube.getY(), iceCube.getSL(), depth);

iceCube doesn't exist at this point - you're just declaring it now - so how can you be calling methods on it?

Ok, I get that. If I can't use methods on it to pass it the right variables for the constructor, how can I use it?

Build it first with dummy local variables and then pass them in?? That seems sloppy, and I probably don't know how to do that anyway.

### #19 jon.kiparsky

• Beginner

Reputation: 11022
• Posts: 18,805
• Joined: 19-March 11

## Re: Unresolved and not visible with inheritance and objects

Posted 26 March 2013 - 02:24 PM

You're getting values from the user... you could use those. Or, if you've constructed those values into some object, like a Point, you could use the object that already exists. (point.getX(), point getY(), etc., are all fine, as long as you've already created point. otherwise, they're kind of, um, pointless... )

### #20 HGPolumbus

Reputation: 0
• Posts: 106
• Joined: 22-March 13

## Re: Unresolved and not visible with inheritance and objects

Posted 26 March 2013 - 02:24 PM

like this?

public static Square createSquare(Point myPoint) {

double sideLength = 0.0;
int dummyX;
int dummyY;

while (true) {
try {
sideLength = Integer.parseInt(JOptionPane.showInputDialog(null,
"The length of one side of the square."));
} catch (NumberFormatException z) {
JOptionPane.showMessageDialog(null, "Sorry, invalid input.",
"!!! INVALID !!!", JOptionPane.ERROR_MESSAGE);
continue;
}
if (sideLength < 0) {
JOptionPane.showMessageDialog(null,
"The length of the side cannot be less than 0.",
"!!! INVALID !!!", JOptionPane.ERROR_MESSAGE);
continue;
}

break;

}
dummyX = myPoint.getX();
dummyY = myPoint.getY();
Square mySquare = new Square(dummyX, dummyY, sideLength);
return mySquare;
}

### #21 jon.kiparsky

• Beginner

Reputation: 11022
• Posts: 18,805
• Joined: 19-March 11

## Re: Unresolved and not visible with inheritance and objects

Posted 26 March 2013 - 02:28 PM

or just
Square mySquare = new Square(myPoint.getX(), myPoint.getY(), sideLength);

### #22 HGPolumbus

Reputation: 0
• Posts: 106
• Joined: 22-March 13

## Re: Unresolved and not visible with inheritance and objects

Posted 26 March 2013 - 02:34 PM

jon.kiparsky, on 26 March 2013 - 02:28 PM, said:

or just
Square mySquare = new Square(myPoint.getX(), myPoint.getY(), sideLength);

Ok. I had that before, but it doesn't work for the Cube class. Says myPoint cannot be resolved. Do I have to pass it both the Point and Square to get it to work like that?

Well I guess I should be able to use mySquare.getX() for that one.

### #23 Flukeshot

• A little too OCD

Reputation: 417
• Posts: 1,030
• Joined: 14-November 12

## Re: Unresolved and not visible with inheritance and objects

Posted 26 March 2013 - 02:36 PM

I believe you're trying to pull this off:
Cube(Square s, int depth) {
//draw3DRect or something
}
Square(Point p, int width, int height){
Cube(this, 10 /*depth*/)
}
Point(p) {//mouse click event or something generates a point p
Square(p,10 /*width*/, 10 /*height*/);
}

is that correct? Ignoring any mistakes I've made (tired) that should be a logically sound inheritance tree to generate a 10x10x10 cube from a single point.. Maybe I'm just rambling.

### #24 HGPolumbus

Reputation: 0
• Posts: 106
• Joined: 22-March 13

## Re: Unresolved and not visible with inheritance and objects

Posted 26 March 2013 - 02:39 PM

Seems to work like this. So I guess when I pass the mySquare into the Cube constructor it carries the x and y with it as well.

public static Cube createCube(Square mySquare) {

double depth = 0.0;

depth = mySquare.getSL();

Cube iceCube = new Cube(mySquare.getX(), mySquare.getY(), mySquare.getSL(), depth);

return iceCube;
}

Flukeshot, on 26 March 2013 - 02:36 PM, said:

I believe you're trying to pull this off:
Cube(Square s, int depth) {
//draw3DRect or something
}
Square(Point p, int width, int height){
Cube(this, 10 /*depth*/)
}
Point(p) {//mouse click event or something generates a point p
Square(p,10 /*width*/, 10 /*height*/);
}

is that correct? Ignoring any mistakes I've made (tired) that should be a logically sound inheritance tree to generate a 10x10x10 cube from a single point.. Maybe I'm just rambling.

No nothing like that yet. I just need to right an output to an optionpane now. Thank you though, I might be able to use that later.

write... I'm spent lol.

Thanks again fellas +'s all around. I'll probably be back

### #25 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Unresolved and not visible with inheritance and objects

Posted 27 March 2013 - 03:49 AM

It is the hen and the egg
Square mySquare = new Square(mySquare.getX(), mySquare.getY(), sideLength);

you cannot invoke getX() on mySquare to create mySquare. mySquare does not exist yet