I created the system of classes describes a shape and its variations. I’ve found out public-access modifier of variable “volume” can be replaced on private so result will not change. It isn’t lead to errors. Part of my code is here:
`
class Shape {
private double volume;
public Shape(double volume) {
this.volume = volume;
}
public double getVolume() {
return volume;
}
}
class SolidOfRevolution extends Shape {
private double radius;
public SolidOfRevolution(double volume, double radius) {
super(volume);
this.radius = radius;
}
public double getRadius() {
return radius;
}
}
class Ball extends SolidOfRevolution {
public Ball(double radius) {
super(Math.PI * Math.pow(radius, 3) * 4 / 3, radius);
}
}`
I considered private access variables aren’t inherited in Java. Why do constructors in sub-classes initialize a private-access variable “volume” as absolutely legitimate part of sub-class? Please tell me what I missed.
I’ve read some similar questions but I still can’t get it: what differ inheritance of class to inheritance of instance?
ekateRINa42S is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.