nikoliazekter
nikoliazekter

Reputation: 757

How do I call an interface method if an object implements that interface?

I have interface Resettable which has method reset(). BaseSystem can implement this interface. Somewhere in code i want to get all systems and reset them if they implement Resettable. Something like:

    for (BaseSystem system : world.getSystems()) {
        if (system instanceof Resettable) {
            system.reset();
        }
    }

However it doesn't work this way. So how can I achieve this?

Upvotes: 1

Views: 182

Answers (1)

Reimeus
Reimeus

Reputation: 159874

Cast first

((Resettable)system).reset();

Upvotes: 5

Related Questions