# Inheritance Problem

• 03-25-2010, 08:44 PM
g2beastie
Inheritance Problem
Hello. I am fairly new to java and programming in general. I'm having a pronlem with an inheritance assignment. Here are instructions:

"Write a program called InheritanceTest.java to support an inheritance hierarchy for class PointSquareCube. Use Point as the superclass of the hierarchy. Specify the instance variables and methods for each class. The private data of Point should be the x-y coordinates, the private data of Square should be the sideLength, the private data of Cube should be depth. Provide applicable accessor methods, mutator methods, toString() methods, area() method and volume() method to all classes. Write a program that instantiates objects of your classes, test all instance methods and outputs each objects area and volume when appropriate. "

I just don't know what else to do at this point, and I believe is to my lack of understanding of constructor methods and inhertiance I think. Here's what I have so far for the three classes:

Code:

```public class Point {                 private static int x;         private static int y;                 public Point(){                 int ex = 0;                 int why = 0;                 x = ex;                 y = why;         }                 public static int getX(){                                 return x;         }         public static int getY(){                                 return y;         }                 public String toString(){                                 String output = "Point class X coordinate = "+ x +", Y coordinate = "                 +y;                                 return output;                         } } public class Square extends Point {                 private static double sideLength;                 public Square(){                                 this.sideLength =         }                 public static double getSideLength(int a, int b){                 double c;                 c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));                                 return c;         }                 public static double getArea(){                                 double area = sideLength * sideLength;                                 return area;         }                 public String toString(){                                 String output = "Square class sidelength = "+ sideLength;                                 return output;         }                 } public class Cube extends Square {         private static double depth, volume, side;                                 public static double getVolume(){                                 volume = (sideLength * sideLength)         } }```
Any hints on what I'm doing wrong? I've tried so many things but I really don't know what I'm doing. Point me in the right direction please. Thank you.:confused:
• 03-25-2010, 09:07 PM
iluxa
Inheritance works quite differently for static fields and methods.

Remove the "static" modifier EVERYWHERE and you should be much better off
• 03-25-2010, 09:08 PM
m00nchile
Ok, the first thing you're doing wrong is your variables are defined as static, i.e. class variables. That means that every Point object (or a subclass object) you create will share the same value of x and y. What you need to do is take static out of the definition of x and y and the methods that use them, as you want to work with different Points, Squares and Cubes that have different properties.
Now, the getSideLength() method in Square class is a good example of a method that should not be static, and it shouldn't be used to calculate the side length, but return the value that is set at object creation.
• 03-25-2010, 09:21 PM
paul pasciak
Understand "static" and use it purposefully.
You are over using "static".
Check out its purpose, and you will see this is true.
But then, your code will probably compile,
and run with these static values as they are.
They just look obviously amature.

early because there are some challenging compiler
error messages that are frustrating to the beginner
who lacks this knowledge.

Code:

```public class Point {                 private static int x;        //  THIS SHOULD NOT BE STATIC         private static int y;        //  THIS SHOULD NOT BE STATIC                 public Point(){                 int ex = 0;                 int why = 0;                 x = ex;                 y = why;         }                 public static int getX(){        //  THIS SHOULD NOT BE STATIC                                 return x;         }         public static int getY(){        //  THIS SHOULD NOT BE STATIC                                 return y;         }                 public String toString(){                                 String output = "Point class X coordinate = "+ x +", Y coordinate = "                 +y;                                 return output;                         } } public class Square extends Point {                 private static double sideLength;        //  THIS SHOULD NOT BE STATIC                 public Square(){                                 this.sideLength =         }                 public static double getSideLength(int a, int b){        //  THIS SHOULD NOT BE STATIC                 double c;                 c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));                                 return c;         }                 public static double getArea(){        //  THIS SHOULD NOT BE STATIC                                 double area = sideLength * sideLength;                                 return area;         }                 public String toString(){                                 String output = "Square class sidelength = "+ sideLength;                                 return output;         }                 } public class Cube extends Square {         private static double depth, volume, side;        //  THIS SHOULD NOT BE STATIC                                 public static double getVolume(){        //  THIS SHOULD NOT BE STATIC                                 volume = (sideLength * sideLength)         } }```
• 03-25-2010, 09:23 PM
pbrockway2
Quote:

Any hints on what I'm doing wrong?
These comments apply for wrong==!(how I would do it), so they might be a little opinionated...

(1) Remove all the "static" keywords. If anything (other than the main() method) in your code is marked static be prepared to explain why you are using static rather than doing it in a more sensible way.

(2) Make x and y in the Point class double rather than int. It will keep things simple if all the dimensional values are of the same type. Likewise the other ints.

(3) Provide arguments to the constructors. The constructor will need the information that goes into defining whatever it is that the instance represents. (The point (0,0) is not particularly special and does not deserve its own, no argument, constructor).

Code:

```    /** Creates a point with given coordinates. */ public Point(double x, double y) {     this.x = x;     this.y = y; }```
(4) Work one step at a time. (1)->(3) apply to the other classes as well (and some other things!) but stop here and write your test program to create a point and use its accessor methods. The idea is to be able to use the compiler to alert you of any bugs it can find so you can fix them, and let you check the output is what you expect. Working in this way allows to ask very focused questions here: what a compiler message means, or why you get the output you do.

-----

It has to be said that there are problems with this assignment. A square IS-NOT-A point. Squares and cubes differ in the number of dimensions they have but not (when I went to school) in the extent they occupy along those dimensions. There's nothing you can do about this, of course. You've been given a pig's ear: good luck!

 Very slow ...;( Sorry for the repetition.