Let us have a set of objects with some common behaviors i.e. they could move up, down, left or right. The exact behaviors (such as how to move and how far to move) depend on the objects themselves.
One common way to model these common behaviors is to define an interface called Movable, with abstract methods moveUp(), moveDown(), moveLeft() and moveRight().
The classes that implement the Movable interface will provide actual implementation to these abstract methods. The code for the interface Movable is straight forward.
interfaceMovable
{
publicvoidmoveUp();
publicvoidmoveDown();
publicvoidmoveLeft();
publicvoidmoveRight();
}
Now write two concrete classes - MovablePoint and MovableCircle - that implement the Movable interface.
MovablePoint
class, declare the instance variable x
, y
, xSpeed
and ySpeed
with package access (i.e., classes in the same package can access these variables directly).MovableCircle
class, use a MovablePoint
to represent its center (which contains four variable x
, y
, xSpeed
and ySpeed
). In other words, the MovableCircle
composes a MovablePoint
, and its radius
.classMovablePointimplementsMovable
{
int x, y, xSpeed, ySpeed; // package access
// Constructor
publicMovablePoint(int x,int y,int xSpeed,int ySpeed) {
this.x = x;
......
}
// Implement abstract methods declared in the interface Movable
@Override
publicvoidmoveUp() {
y -= ySpeed; // y-axis pointing down for 2D graphics
}
......
}
classMovableCircleimplementsMovable
{
private MovablePoint center; // can use center.x, center.y directly because they are package accessible
privateint radius;
// Constructor
publicMovableCircle(int x,int y,int xSpeed,int ySpeed,int radius) {
// Call the MovablePoint's constructor to allocate the center instance.
center =new MovablePoint(x, y, xSpeed, ySpeed);
......
}
......
// Implement abstract methods declared in the interface Movable
@Override
publicvoidmoveUp() {
center.y -= center.ySpeed;
}
......
}
Also add a toString() method in both above classes to print their x and y position in the format shown below: -
[X=value, Y=value] // for MovablePoint
[X=value, Y=value, radius=value] // for MovableCircle
class MovablePoint implements Movable
{
int x, y, xSpeed, ySpeed;
// Constructor
MovablePoint(int x,int y,int xSpeed, int ySpeed){
this.x = x;
this.y = y;
this.xSpeed = xSpeed;
this.ySpeed = ySpeed;
}
// Implement abstract methods declared in the interface Movable
@Override
public void moveUp(){
y-=ySpeed;
}
@Override
public void moveDown(){
y+=ySpeed;
}
@Override
public void moveLeft(){
x-=xSpeed;
}
@Override
public void moveRight(){
x+=xSpeed;
}
@Override
public String toString(){
return "[X="+x+", Y="+y+"]";
}
}
class MovableCircle implements Movable
{
private MovablePoint center;
private int radius;
// Constructor
MovableCircle(int x,int y , int xSpeed,int ySpeed,int radius){
center = new MovablePoint(x,y,xSpeed,ySpeed);
this.radius = radius;
}
// Implement abstract methods declared in the interface Movable
@Override
public void moveUp(){
center.y -=center.ySpeed;
}
@Override
public void moveDown(){
center.y +=center.ySpeed;
}
@Override
public void moveLeft(){
center.x -= center.xSpeed;
}
@Override
public void moveRight(){
center.x += center.xSpeed;
}
@Override
public String toString(){
return "[X="+center.x+", Y="+center.y+", radius="+radius+"]";
}
}