Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it possible to block/deny a cast conversion in Java?
    primarykey
    data
    text
    <p>I have the code of a simple game, where an AgentInterface must be implemented in order to create an agent controller for one of the characters in the game. GameState is a class the implements GameStateInterface, and an object that implements this interface can be passed to the agent, so the agent can read and analyze the data from game state, and the agent must return the appropriate action (returned as an int) that the character should take.</p> <p>This is the AgentInterface that agents must implement:</p> <pre><code>public interface AgentInterface { // the return value specifies the direction of the joystick public int action(GameStateInterface gs); } </code></pre> <p>Running the game with an agent called MyAgent:</p> <pre><code> GameState gs = new GameState(); AgentInterface agent = new MyAgent(); while (true) { // more code here int bestAction = agent.action(gs) // more code here } </code></pre> <p>But, there is some information in GameState that the agent should NOT be able to access, since that would be cheating for the controller. But, doing a cast conversion from GameStateInterface to GameState would allow the agent to access information that is not defined in the GameStateInterface, like this:</p> <pre><code>public MyAgent implements AgentInterface { public int action(GameStateInterface gs) { int nLives = ((GameState) gs).nLivesRemaining; // IS IT POSSIBLE TO DENY/PREVENT THIS CAST?? // Do more stuff here return BestAction; } } </code></pre> <p>My question would be, is it possible to block a cast conversion? I know polymorphism is one of the main features of Java and Object-Oriented Programming Languages, but in cases like this I would like to avoid cast conversions.</p> <p>I know this can be solved in many other ways, but I was curious to know if it is possible to do this.</p> <p>Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload