0
votes

Today I decided to make a top down based game using Java. I have already made the window and included the Jframe. But I found a problem on creating the GameObject in the Rectagle GetBounds(); . I don't know what really is because I am a beginner and I know the basics of java :( .

If anyone can help me to resolve this problem I give the code example below:

package example;

import java.awt.Graphics;

import java.awt.Rectangle;

public abstract class GameObject {
protected int x, y;
protected float velX = 0, velY = 0;

public GameObject(int x, int y) {
    this.x = x;
    this.y = y;
}

public abstract void tick();
public abstract void render(Graphics g);
public abstract void Rectangle getBounds();

public int getX() {
    return x;
}

public void setX(int x) {
    this.x = x;
}

public float getVelX() {
    return velX;
}

public void setVelX(float velX) {
    this.velX = velX;
}

public float getVelY() {
    return velY;
}

public void setVelY(float velY) {
    this.velY = velY;
}
} 

The code causes the following errors:

Illegal modifier for the field Rectangle; only public, protected, private, static, final, transient & volatile are permitted Return type for the method is missing Syntax error, insert ";" to complete FieldDeclaration This method requires a body instead of a semicolon void is an invalid type for the variable Rectangle

Note: I'm using Java SE-8 and Eclipse Oxigen.

1
If getBounds() returns a Rectangle it should not have void. - Loris Securo
void and Rectangle are both return types of getBounds. A method can't have multiple return types. - Bernhard Barker
public abstract void Rectangle getBounds(); should be public abstract Rectangle getBounds(); - MadProgrammer

1 Answers

0
votes

If you want to create an instance of GameObject (like this: new GameObject()) then GameObject class must not be abstract.

You cannot create an instance of abstract class. You can only create a class that inherits from abstract class and implements all or some methods.

Here you can either implement methods tick(), render(Graphics g) and getBounds() or create a new class that inherits from GameObject (public class GameObjectImp extends GameObject) and implement methods there.