0
\$\begingroup\$

Im creating a game using LWJGL and just as a bit of organising I made an Abstract class called Game so I could extend Game in my main class The game class is like so -

public abstract class Game {
    public abstract void init();
    public abstract void render();
}

Really simple stuff, and my plan was to call the render and init method in my DisplayManger class so every whenever I my display is created and updates it runs these methods so all games extending my game class with have their methods run. My approach was like this but as you probably have figured out you cannot a abstract method from another class nor from a static method from inside my abstract, it needs to be static to call from another class. Here was my approach -

public class DisplayManager {
    public DisplayManager(int width, int height) {
        try {
            Display.setDisplayMode(new DisplayMode(width,height));
            Display.create();
        } catch (LWJGLException e) {
            e.printStackTrace();
        }
        Game.init();    
        while (!Display.isCloseRequested()) {
            Game.render();
            Display.update();
        }
        Display.destroy();
    }
}

Does anyone know how I can call these two methods from this Display class?

\$\endgroup\$
2
\$\begingroup\$

An abstract method is a method without a body. So you can't just call an abstract method of an abstract class (you cannot instantiate an abstract class directly). If you want to have your abstract game class with the abstract methods you need to have a class that extends this game class and specifically implements these methods without the abstract keyword.

Or you remove the abstract keyword from init and render and then the subclass of your Game class doesn't have to implement these methods but will take the one defined in your Game class.

Alternatively you can remove the abstract keyword from the methods in your game class but still @Override them in your subclass. That way you can define a default behavior for init and render and still change it in a subclass if needed.

public abstract class Game {

    public Game() {};

    public void init()
    {
        // do init stuff
    }

    public void render()
    {
        // do render stuff
    }
}




public class specificGame extends Game {

    public specificGame() {};

    // no need to implement init and render. But you can still override them

    @Override
    public void init()
    {
        // spefic init stuff
    }

    @Override
    public void render()
    {
        // specific render stuff
    }

}

More about abstract classes and methods https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html

Is this what you want?

This is the main file:

package devex;

import devex.SubClass;

public class MainClass
{

    public static void main(String[] args)
    {
        SubClass.init();
        AClass.init();
    }
}

The this is the abstract class defining a default init

package devex;

public abstract class AClass
{

    public static void init() 
    {
        System.out.println("AClass init");
    }

}

This subclass extends AClass and overwrites init

package devex;

public class SubClass extends AClass
{

    public static void init()
    {
        System.out.println("Subclass init");
    }

}
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Yes i could do this but the problem still remains they must be static to be called from another class and if you make these methods static the @Override annotation must be removed and the class fails to function. \$\endgroup\$ – Hamish May 18 '16 at 9:04
  • \$\begingroup\$ Why do they need to be static? Or why don't you want to instantiate a Game class or a subclass thereof? \$\endgroup\$ – Philipp May 18 '16 at 9:12
  • \$\begingroup\$ First, for a method to be called by another class it must be public and static second, you cannot instantiate an abstract class \$\endgroup\$ – Hamish May 18 '16 at 9:13
  • \$\begingroup\$ See my edit, but I'm not sure what your goal is. \$\endgroup\$ – Philipp May 18 '16 at 9:26
  • \$\begingroup\$ No, I need to call the abstract method so everything that is extended will get called as well \$\endgroup\$ – Hamish May 18 '16 at 9:28
0
\$\begingroup\$

The fact that your methods are abstract is secondary, here -- the main issue is that you're calling the methods as if they were declared static:

Game.init()
Game.render()

Instead, what you need in order to call these methods is an instance of the Game class and invoke those methods on that object:

Game game = new SubclassOfGame();
game.init()
game.render()
| improve this answer | |
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.