Unresolved and not visible with inheritance and objects

Posted 26 March 2013 - 08:37 AM

Hello again. I'm working on a program where I need to collect 2 point coordinates, the length of a side of a square, and the depth of a cube. The program should then print it all out, each class inheriting from the last. My issue I'm having is building my driver class so that I can call a method to get the input for each one before printing from the objects. Here's where I'm at:

I got most of it done but I can't figure out the part where you put it together. How do I pull the variables out of the superclass to use them when I build the subclass object? Shouldn't they just be in there from inheriting?

Re: Unresolved and not visible with inheritance and objects

But assuming that this is the design that you want, or have to use, I believe your main issue is the use of private access on x and y. Try protected.

This is a little counterintuitive, maybe: since Square is a Point, it has an x and a y, and they are its fields, so it should have access to them. I totally agree that this would make more sense. However, that's not actually how it works: inherited fields are actually not visible unless they're protected or public, and you don't want them to be public.

Re: Unresolved and not visible with inheritance and objects

But assuming that this is the design that you want, or have to use, I believe your main issue is the use of private access on x and y. Try protected.

This is a little counterintuitive, maybe: since Square is a Point, it has an x and a y, and they are its fields, so it should have access to them. I totally agree that this would make more sense. However, that's not actually how it works: inherited fields are actually not visible unless they're protected or public, and you don't want them to be public.

Is there another option to keep them private? Forget about my create....() methods? Would I have to sloppily put all the optionpane stuff in the main then?

Thanks for the replies.

Even if it were to work with protected, I have to call the next one

Square mySquare = createSquare(myPoint.x, myPoint.y, sideLength);

and sideLength cannot be resolved...

because it doesn't exist until after the method is done and the Square object created

Re: Unresolved and not visible with inheritance and objects

Posted 26 March 2013 - 09:39 AM

Yes, those are two different methods. One is your local method which gets some information from the user and calls the Square constructor, the other is the Square constructor. One takes two ints and a double, the other takes a Point.

There's nothing surprising about the fact that both of those calls work.

Re: Unresolved and not visible with inheritance and objects

Posted 26 March 2013 - 02:07 PM

If you have a private field foo in an object, and you want to let the world look at it, give that object a getFoo method. If you have a private field foo in an object and you want to let the world poke at it, give the object a setFoo method.

And at that point, you've made the object public, so there's no point in calling it private at all...

Re: Unresolved and not visible with inheritance and objects

Posted 26 March 2013 - 02:07 PM

Your Point class has a getX() and getY() that can be called on any element of your hierarchy (Point, Square, Cube)
you just need a public getSideLength() for the Square and a public getDepth() for the Cube

Re: Unresolved and not visible with inheritance and objects

Posted 26 March 2013 - 02:11 PM

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

If you have a private field foo in an object, and you want to let the world look at it, give that object a getFoo method. If you have a private field foo in an object and you want to let the world poke at it, give the object a setFoo method.

And at that point, you've made the object public, so there's no point in calling it private at all...

I did that, I think... but the error under my objects like mySquare.getX() says that it is not initialized. I thought creating it was initializing it....