-2

I am still a novice in Java. My question may be really basic.

I have a class super class Box,

package chapter8;

public class Box {

    double width;
    private double height;
    private double depth;

    Box(double w, double h, double d) {
        width = w;
        height = h;
        depth = d;
    }

    double volume() {
        return width * height * depth;
    }
}

BoxWeight is subclass for Box super class:

package chapter8;

public class BoxWeight extends Box {

    double weight;

    BoxWeight(double w, double h, double d, double m){
        super(w, h, d);
        weight = m;
    }
}

Now i have main in DemoBoxWeight

package chapter8;

public class DemoBoxWeight {

    public static void main(String[] args) {

        BoxWeight myBox1 = new BoxWeight(2, 3, 4, 5);

        System.out.println("Volume1 :" + myBox1.volume());
        System.out.println("Weight1 :" + myBox1.weight);
        System.out.println("Widht1: " + myBox1.width);
        System.out.println("Depth1: " + myBox1.depth); // as depth is private, it is not accessible
    }
}

As height and depth are defined as Private so DemoBoxWeight which actually passes the value of these variables is not able to access it. I know i can change the Private to default/public but is there another way also so that the class that is passing the values actually can access it?

PS: As i am new my terminology can be wrong and my question really stupid

prabh
  • 336
  • 1
  • 3
  • 16
  • 1
    Basically, you'll want to use getters and setters; public methods to fetch or modify your private variables. – Chris Forrence Apr 28 '13 at 17:34
  • http://stackoverflow.com/questions/2036970/tutorial-on-getters-and-setters – Brian Roach Apr 28 '13 at 17:34
  • The first question you need to answer is should DemoBoxWeight be able to access the height and weight or are those too close to implementation details that should be hidden from other classes? If they should be available, then `public int getHeight()` would make the height readable. – Mike Samuel Apr 28 '13 at 17:36
  • Refer documentation http://docs.oracle.com/javase/tutorial/java/javaOO/variables.html – commit Apr 28 '13 at 17:38
  • i agree, but if edit the code and initialise the Private variables height and depth, say = 1 and then in the class DemoBoxWeight when i am creating BoxWeight Object, i can override the values height and depth already initilaised as 1 to whatever i am passing say 3,4 , eventhough they are protected they get set without the getter/setter. – prabh Apr 28 '13 at 17:46

5 Answers5

5

The usual way of doing this is to write getters and setters like this:

public double getHeight()
{
    return this.height;
}

public void setHeight(double height)
{
    this.height = height;
}

You can remove the setter if you don't want the value to be changed from outside the class.

MAV
  • 7,260
  • 4
  • 30
  • 47
  • i agree, but if edit the code and initialise the Private variables height and depth, say = 1 and then in the class DemoBoxWeight when i am creating BoxWeight Object, i can override the values height and depth already initilaised as 1 to whatever i am passing say 3,4 , eventhough they are protected they get set without the getter/setter – prabh Apr 28 '13 at 17:51
  • @prabh I am not sure I understand the problem. In `DemoBoxWeight` you initialize an instance of `BoxWeight` with the constructor you have provided. After you have created the object `DemoBoxWeight` can no longer change the values. If you do not want the initial value to be changed when constructing objects, don't provide a constructor that enable this. – MAV Apr 28 '13 at 17:56
1

Basically, you need to provide access methods to your class properties.

There are 2 access methods - getters and setters and these are the standard way of providing read-write access to your class according to Java Bean definition

sanbhat
  • 17,522
  • 6
  • 48
  • 64
  • i agree, but if edit the code and initialise the Private variables height and depth, say = 1 and then in the class DemoBoxWeight when i am creating BoxWeight Object, i can override the values height and depth already initilaised as 1 to whatever i am passing say 3,4 , eventhough they are protected they get set without the getter/setter – prabh Apr 28 '13 at 17:58
1

Here's the documentation on encapsulation (which is what you're dealing with): http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html.

Geoff
  • 773
  • 1
  • 6
  • 13
0

This is mostly done by creating so called getters.

public int getHeight(){
  return this.height;
}

The idea is that (instead of making things public) that whenever in the future you want to change the internal representation of your box, you can do this without disturbing users depending on height.

By example:

Lets say you want to store the diagonals instead of depth. Or you might want to use floats, or other number types.

getHeight might start looking as this:

public int getHeight(){
  return diagonalstoHeight(diagonal1, height, width);
}

And no one will ever now. You should also read about encapsulation and information hiding

tgoossens
  • 9,676
  • 2
  • 18
  • 23
  • i agree, but if edit the code and initialise the Private variables height and depth, say = 1 and then in the class DemoBoxWeight when i am creating BoxWeight Object, i can override the values height and depth already initilaised as 1 to whatever i am passing say 3,4 , eventhough they are protected they get set without the getter/setter – prabh Apr 28 '13 at 17:59
0

Change private to protected.

The protected modifier allows all subclasses in the class hierarchy to access an instance variable without the need to use getter or setter methods.

It still denies other classes (that are outside the class hierarchy) from accessing it, so encapsulation is still accounted for.

Sean Rogers
  • 499
  • 4
  • 4