Untitled

mail@pastecode.io avatar
unknown
java
2 years ago
853 B
1
Indexable
Never
package com.JavaClass;

public class Main {
    public static void main(String[] args) {
        Object[] myArray = new Object[4];
        myArray[0] = new SmokeAlarm();
        myArray[1] = new SmokeAlarm();
        myArray[2] = new FireEmergency();
        myArray[3] = new FireEmergency();
        for (int i = 0; i < 4; i++) {
            if (myArray[i] instanceof isEmergency) {
                System.out.printf("Index %d is an instance\n", i);
                ((FireEmergency) myArray[i]).soundSiren();
            } else
                System.out.printf("Index %d is not an instance\n", i);
        }
    }
}


interface isEmergency {
    void soundSiren();
}

class FireEmergency implements isEmergency {
    public void soundSiren() {
        System.out.println("Siren Sounded");
    }
}

class SmokeAlarm {
}