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