I am designing a simple security system, which has sensors, that sense the sound, motion or smoke and then sound the alarm and switch on and off the lights according to what was sensed. Using OOP concepts here; I ran into a situation where I am establishing HAS-A relation and runtime polymorphism at the same time(where I place a child class reference into the object of parent class). I want to know whether it is a safe choice or not? I have a class named Light which has 3 subclasses RegLight, GreenLight and NormalLight
public class SecuritySystem {
public void switchOnOffLights(int sensorNumber){
Light light;
if (sensorNumber == 1){
light = new NormalLight();
light.lightFunctionality();
} else if (sensorNumber == 2){
light = new RedLight();
light.lightFunctionality();
}else if (sensorNumber == 3){
light = new GreenLight();
light.lightFunctionality();
}
}
}// end Security System class