Java Reference
In-Depth Information
/** Returns the area of an object */
double calculateArea();
}
Then, if Shape implements Measurable , the subclasses of Shape would also implement Measurable .
public abstract class Shape implements Measurable {
private String color;
 
public Shape(String color) {
this.setColor(color);
}
 
public String getColor() {
return this.color;
}
 
public void setColor(String color) {
this.color = color;
}
}
 
public class Circle extends Shape {
private double radius;
private final double pi = 3.14159;
 
public Circle(String color, double radius) {
super(color);
this.setRadius(radius);
}
 
public double getRadius() {
return this.radius;
}
 
private void setRadius(double radius) {
this.radius = radius;
}
 
@Override
public double calculateArea() {
return pi * this.getRadius() * this.getRadius();
}
 
@Override
public double calculatePerimeter() {
return 2 * pi * this.getRadius();
}
 
}
The following exercise will give you an opportunity to try out interfaces for yourself. While you
work through it, keep in mind the similarities and differences between inheritance and interfaces.
As you gain experience, you'll see how one or the other might be better suited to certain situations.
Search WWH ::




Custom Search