Giving partial access to a class using an interface

The solution is an interface. While it is possible to pass a reference of the GameEngine from GameEngine, to GameState this isn't desirable. What we need is a way to give GameState, direct but limited control. If it had a full reference to GameEngine it is likely that as the project progressed it would end up creating problems because GameState had too much access to GameEngine. For example, what if GameState decided to pause the game at the wrong time?

Interface refresher

If you think back to Chapter 8, Object-Oriented Programming, an interface is a class without any method bodies. A class can implement an interface and when it does, it must provide the body (including the code) for that method ...

Get Learning Java by Building Android Games - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.