As we know we cannot instantiate, but i have gone through a code and found that object is created and how?

51 views Asked by At

I have this interface

public interface GamingConsole {
    void up();
    void down();
    void left();
    void right();
}

This is the class where we are instantiating

public class GameRunner {
    private GamingConsole game;  // this is interface that we are instantiating 

    public GameRunner(GamingConsole game) {
        this.game = game;
    }
    public void run() {
        System.out.println("Running Game" + game);
        game.up();
        game.down();
        game.left();
        game.right();
    }
}

how we are able to run private GamingConsole game; ??

I am calling in this class

public class AppGamingBasicJava {

    public static void main(String[] args) {
        // var game = new MarioGame();
        // var game = new SuperContraGame();
        var game = new PacManGame();
        var gameRunner = new GameRunner(game);
        gameRunner.run();
    }

}

I was learning spring(loose coupling specifically), during this gone through a problem in which we are instantiating interface and theoretically we cannot do that, i wanted to know what actually is happening here, what we are doing here and how my code is working.

I have tried finding solution multiple places but not able to find exact.

0

There are 0 answers