I am fairly new to java so I tried to implement an example and use abstract classes but my lack of OO knowledge makes me wonder why I cannot use a private variable from the abstract class in a "concrete" class extending it.
Here is my code:
abstract class Equation{
private double[] c;//oefficient
public static int degree(double[] coeff) {
return coeff.length;
}
public abstract double[] solve();
}
class QuadraticEquation extends Equation{
public double[] solve() {
double[] solution;
double discriminant = c[1]*c[1]-4*c[0]*c[2];
if (discriminant < 0) {
solution = new double[] {Double.NaN,Double.NaN};
}
else {
solution = new double[] {(c[1]+Math.sqrt(discriminant))/(2*c[0])
,(c[1]-Math.sqrt(discriminant))/(2*c[0])};
}
return solution;
}
}
The Error I get is
c has private access in Equation
i could resolve this by making c a public variable, but I guess there is a better way to do this.
Bottom Line: How do I access the variable c.
cmeans with a comment, why not name your variablecoefficientin the first place? Why is this variable defined in the base class since it doesn't use it at all?