I have thought a lot about this but can't come to a proper solution. Also, it's hard to google things like this.

Note: The code itself is irrelevant, it's about how to do such a thing in general.

I have a class named `Creature` which has the following method:

```
private void move(int direction, LyxCamera camera, MapLayers mapLayers, ArrayList<Creature> entities) {
    if (!moving) {
        if (direction != NONE) {
            this.direction = direction;

            int goalGridPosX = gridPosX;
            int goalGridPosY = gridPosY;

            if (direction == UP) {
                goalGridPosY = gridPosY + 1;
            } else if (direction == RIGHT) {
                goalGridPosX = gridPosX + 1;
            } else if (direction == DOWN) {
                goalGridPosY = gridPosY - 1;
            } else if (direction == LEFT) {
                goalGridPosX = gridPosX - 1;
            }

            if (goalIsFree(goalGridPosX, goalGridPosY, mapLayers, entities)) {
                stop();
                moving = true;

                /* FROM HERE

                if (direction == UP) {
                    if (goalGridPosY > camera.getGridPosY() + 4) {
                        camera.move(direction, speed);
                    }
                } else if (direction == RIGHT) {
                    if (goalGridPosX > camera.getGridPosX() + 8) {
                        camera.move(direction, speed);
                    }
                } else if (direction == DOWN) {
                    if (goalGridPosY < camera.getGridPosY() + 4) {
                        camera.move(direction, speed);
                    }
                } else if (direction == LEFT) {
                    if (goalGridPosX < camera.getGridPosX() + 7) {
                        camera.move(direction, speed);
                    }
                }

                TO HERE*/

                gridPosX = goalGridPosX;
                gridPosY = goalGridPosY;

                step();
            }
        }
    }

```

I marked the code I want to extract to a subclass with "FROM HERE TO HERE".

I could place the code in a method and call the empty method, then overwrite the method in the subclass. But that really doesn't seem to be a proper solution. I could also copy the whole method and just remove the marked area in the superclass. This also doesn't seem to be best practice.

I'm sure you guys know exactly what I'm looking for. Oh, and if you have general suggestions to improve the code, feel free to let me know!

Maybe I'm just too tired already, and a solution will come to me in my dreams. Anyways, I'm very curious to see your suggestions!

Many thanks!

Regards Lyxodius

EDIT:

First, I'm very thankful that you guys took the time to give me answers so detailed!
My solution was inspired by all of your answers from here and stackoverflow!

**Superclass "Creature.java":**

```
private void move(int direction, LyxCamera camera, MapLayers mapLayers,
        ArrayList<Creature> entities) {
    if (!moving && direction != NONE) {
        this.direction = direction;

        int goalGridPosX = gridPosX;
        int goalGridPosY = gridPosY;

        switch (direction) {
        case UP:
            goalGridPosY++;
            break;
        case RIGHT:
            goalGridPosX++;
            break;
        case DOWN:
            goalGridPosY--;
            break;
        case LEFT:
            goalGridPosX--;
            break;
        }

        if (goalIsFree(goalGridPosX, goalGridPosY, mapLayers, entities)) {
            prepareMoving(goalGridPosX, goalGridPosY);
        }
    }
}

protected void prepareMoving(int goalGridPosX, int goalGridPosY) {
    startMoving(goalGridPosX, goalGridPosY);
}

private void startMoving(int goalGridPosX, int goalGridPosY) {
    stop();

    moving = true;

    gridPosX = goalGridPosX;
    gridPosY = goalGridPosY;

    step();
}

```

**Subclass "Player.java":**

```
@Override
protected void prepareMoving(int goalGridPosX, int goalGridPosY) {
    super.prepareMoving(goalGridPosX, goalGridPosY);
    moveCamera();
}

private void moveCamera() {
    if (direction == UP
            && gridPosY > camera.getGridPosY() + MINIMUM_VERTICAL_OFFSET) {
        camera.move(direction, speed);
    } else if (direction == RIGHT
            && gridPosX > camera.getGridPosX()
                    + MINIMUM_HORIZONTAL_OFFSET_RIGHT) {
        camera.move(direction, speed);
    } else if (direction == DOWN
            && gridPosY < camera.getGridPosY() + MINIMUM_VERTICAL_OFFSET) {
        camera.move(direction, speed);
    } else if (direction == LEFT
            && gridPosX < camera.getGridPosX()
                    + MINIMUM_HORIZONTAL_OFFSET_LEFT) {
        camera.move(direction, speed);
    }
}

```