I can't return the result in an abstract method because it tells me that the variables are private despite having configured the methods get
andset
Standard class:
public class Triangulo extends Poligono implements FigueGeometrica {
public Triangulo(double altura, double base) {
super(altura, base);
setBase(Integer.parseInt(JOptionPane.showInputDialog(null,"Base de triangulo")));
setAltura(Integer.parseInt(JOptionPane.showInputDialog(null,"Altura de triangulo")));
getBase();
getAltura();
}
@Override
public double calcularArea() {
return (base*altura)/2;
//aqui es donde ocurre el problema.
}
@Override
public double calcularPerimetro() {
return 0;
}
Abstract class:
public abstract class Poligono implements FigueGeometrica {
private double altura;
private double base;
public void setAltura(double altura) {
this.altura = altura;
}
public void setBase(double base) {
this.base = base;
}
public double getAltura() {
return altura;
}
public double getBase() {
return base;
}
public Poligono(double altura, double base) {
}
}
Interface:
public interface FigueGeometrica {
double calcularArea();
double calcularPerimetro();
}
This happens because private attributes are not inherited and are only accessible by the class that contains them. If you want to access them, it must be with their getter and setter
The correct way to implement it would be by calling the function where you want to perform the calculations and passing the values through calls to the getter of the variables in question.