Reader small image

You're reading from  Learning Java by Building Android Games - Third Edition

Product typeBook
Published inMar 2021
Reading LevelBeginner
PublisherPackt
ISBN-139781800565869
Edition3rd Edition
Languages
Right arrow
Author (1)
John Horton
John Horton
author image
John Horton

John Horton is a programming and gaming enthusiast based in the UK. He has a passion for writing apps, games, books, and blog articles. He is the founder of Game Code School.
Read more about John Horton

Right arrow

Giving partial access to a class using an interface

The solution is an interface. While it is possible to pass a reference of 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 has 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 or methods. Furthermore, when a class implements an interface, it is an object of that type. When a class is a specific type, it can be used polymorphically as that type even if it is other types as well. Here are some examples of...

lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Learning Java by Building Android Games - Third Edition
Published in: Mar 2021Publisher: PacktISBN-13: 9781800565869

Author (1)

author image
John Horton

John Horton is a programming and gaming enthusiast based in the UK. He has a passion for writing apps, games, books, and blog articles. He is the founder of Game Code School.
Read more about John Horton