Danilo Setton
Danilo Setton

Reputation: 713

Java interface - Return on method call

I've been struggling a couple of days trying to understand how the code below works.

I simply have: an abstract class:

public abstract class Screen {

    protected final Game game;

    public Screen(Game game) {
        this.game = game;
    }

    public abstract void update(float deltaTime);
    public abstract void paint(float deltaTime);
    public abstract void pause();
    public abstract void resume();
    public abstract void dispose();
    public abstract void backButton();

}

and an interface:

public interface Game {
    public void setScreen(Screen screen);
    public Screen getInitScreen();
}

I understood that the interface methods have no body because they say what classes can do, not how.

Then, when I call the method below from a class that extends the Screen abstract class:

game.getInitScreen();

What exactly this method will return? A new Screen? But there is nothing on this Screen class...no canvas, no SurfaceView...what's the point of such call?

Upvotes: 2

Views: 110

Answers (1)

Elliott Frisch
Elliott Frisch

Reputation: 201429

Because, at run-time, there will be a class that provides a concrete implementation of a Screen. Exactly what that class is could be determined with something like game.getInitScreen().getClass().getName()

Upvotes: 1

Related Questions