Clase11(Herencia).ppt
=====================
class Circulo extends Circunferencia
Circulo
area
Circulo
radio
Circunferencia
perimetro
Circunferencia


Solución 1. Con clases independientes
class Circulo{
protected double r;
public Circulo(double x){
r=x; if(r<=0) U.abortar(“radio<=0”);
}
public double area(){return Math.PI*r*r;}
public double perimetro(){return 2*Math.PI*r;}
}
class Cuadrado{
protected double a;
public Cuadrado(double x){
a=x; if(a<=0) U.abortar(“lado<=0”);
}
public double area(){return a*a;}
public double perimetro(){return 4*a;}
}

Solución 2. Con jerarquía de clases
class Figura{
protected double x;
public Figura(double x){
this.x=x; if(x<=0) U.abortar(“<=0”);
}
}
class Circulo extends Figura{
public Circulo(double x){super(x);}
public double area(){return Math.PI*x*x;}
public double perimetro(){return 2*Math.PI*x;}
}
class Cuadrado extends Figura{
public Cuadrado(double x){super(x);}
public double area(){return x*x;}
public double perimetro(){return 4*x;}
}

Solución 3. Con clase abstracta que obliga a redefinir métodos en clases extendidas

abstract class Figura{
protected double x;
public Figura(double x){
this.x=x; if(x<=0) U.abortar(“debe ser >0”);
}
abstract public double area();
abstract public double perimetro();
}
Notas
no permite crear objetos (no admite new Figura() )
debe tener al menos un método abstracto:
abstract encabezamiento;
obliga a clases extendidas a redefinir métodos abstractos



class Rectangulo extends Figura{
protected double y;
public Rectangulo(double x,double y){
super(x); this.y=y; if(y<=0) U.abortar(“<=0”);
}
public double area(){return x*y;}
public double perimetro(){return 2*(x+y);}
}
class Triangulo extends Figura{
protected double y,z;
public Triangulo(double x,double y,double z){
super(x); this.y=y; this.z=z;
if(y<=0 || z<=0 || x+y<=z || x+z<=y || y+z<=x)
U.abortar(“no forman triangulo”);
}
public double perimetro(){return x+y+z;}
public double area(){
double s=(x+y+z)/2;
return Math.sqrt(s*(s-x)*(s-y)*(s-z));
}}