Marco Karaki
Marco Karaki

Reputation: 65

Casting from an interface object to an object of a class that implements it?

I am trying to cast an interface object:

ScreenController startScreenController = nifty.getScreen("start").getScreenController();

(ScreenController is an interface)

to an object of a class named PrisonStartScreenControl:

startScreenController(PrisonStartScreenControl).setNifty(nifty);

^ but this line causes an error. I know that the startScreenController object is equal to a PrisonStartScreenControl, so how would I be able to cast the interface to its implementor-class?

Upvotes: 1

Views: 74

Answers (1)

Dan Saunders
Dan Saunders

Reputation: 280

The cast needs to be on the left side of the variable to be cast, not the right, e.g:

((PrisonStartControl)startScreenController).setNifty(nifty);

But, why are you casting anyway? setNifty() should be a method on the interface so no need to cast.

Upvotes: 2

Related Questions