Examples class: Interfaces, ArrayList,
Interfaces can be quite an abstract concept at first. In this example class we will go through an additional example that will hopefully help to make things clearer!
– Consider writing a game, the graphics for which are all simple shapes
– In the game, we will need to do things with these shapes (move them, grow them, shrink
Copyright By PowCoder代写 加微信 powcoder
them (which is just growing them in reverse). The shapes are:
– Rectangle – Triangle
A circle is defined by its centre and radius. A rectangle by the coordinates of its top left and bottom right corners. A triangle by the coordinates of its three points. I.e. there aren¡¯t any shared attributes that might make a class hierarchy sensible.
In this step, we will make the first two classes (circle & rectangle) and a main method that creates them and a play method that somehow plays the game (in this case, it just moves and grows some things).
Now we will identify what functionality the shapes share. Some important points:
1. The shapes don¡¯t share how they do things. Just the fact that they do them. E.g. they all move and they all grow. How the shapes grow is irrelevant — it¡¯s up to them (or whoever
made them). This is perfect for an interface — we just want to formally define that particular
classes do something.
Once we¡¯ve identified this, we can formalise it through an interface (Shape). And then change our main method to not have to worry about storing different shapes separately — they can all be stored against Shape references.
Now we can change our play method to make it so much neater. It doesn¡¯t care about different kinds of shapes because it only cares that it is given things that can be moved or grown! I.e. it cares about the existence of some particular functionality in the objects it is given, not what particular class those objects are, or how that functionality is implemented.
Finally, we can add Triangle. The key point here is that without the interface we would need to change the play method (to allow it to be passed Triangle objects and to deal with them) but with the interface it (and any other parts of the game) don¡¯t need to be changed at all.
We will develop the code live in the class. For completeness, example code is included below… public class Circle {
private int x;
private int y;
private int radius;
public Circle(int x, int y, int r) {
this.x = x; this.y = y; this.radius = r;
public void move(int changeX, int changeY) {
x += changeX;
y += changeY; }
public void grow(int factor) { radius *= factor;
public class Rectangle { private int x1;
private int y1;
private int x2;
private int y2;
public Rectangle(int x1, int y1, int x2, int y2) {
this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2;
public void move(int changeX, int changeY) {
x1 += changeX; x2 += changeX; y1 += changeY; y2 += changeY;
public void grow(int factor) {
int width = x2 – x1; int height = y2 – y1; width *= factor; height *= factor;
x2 = x1 + width;
y2 = y1 + height; }
Note the big differences in the attributes for each class, and how the methods are implemented. Here¡¯s a main and play method:
public class ShapeMain {
public static void main(String[] args) {
// Make some circles
Circle[] circles = new Circle[3]; circles[0] = new Circle(1,1,2); circles[1] = new Circle(2,2,1); circles[2] = new Circle(3,4,2);
// Make some rectangles
Rectangle[] rectangles = new Rectangle[2]; rectangles[0] = new Rectangle(0,0,1,1); rectangles[1] = new Rectangle(1,2,2,1);
play(circles, rectangles); }
public static void play(Circle[] circles, Rectangle[] rectangles) { // Move them all 3 to the right
for(int i=0;i