I know its a weird question but I was asked this in an interview by the CEO of a software house, First, he asked if a remote could be considered an Object, If yes then explain why? If it is an object then can it be polymorphic in nature (in the context of OOP) ? I said no because it can only switch on/off an AC, but he said what if I use it as a weapon and throw it at someone? Does that make it polymorphic?
Can somebody please explain this?
if a remote could be considered an Object
yes, because you can design/model any real world item https://www.educative.io/blog/object-oriented-programming
can it be polymorphic in nature (in the context of OOP)
yes, you can create abstract class to share behaviours among derived classes. After share behaviour is defined, then you can implement concrete remote controls like Electrolux, LG, Samsung
public abstract class RemoteControl
{
public abstract void TurnOnOff();
}
public class RemoteControl_Electrolux : RemoteControl
{
public override void TurnOnOff()
{
Console.WriteLine("Electrolux is turned on/off");
}
}
public class RemoteControl_Samsung : RemoteControl
{
public override void TurnOnOff()
{
Console.WriteLine("Samsung is turned on/off");
}
}
public class RemoteControl_LG : RemoteControl
{
public override void TurnOnOff()
{
Console.WriteLine("LG is turned on/off");
}
}
and use it:
List<RemoteControl> remoteControls = new List<RemoteControl>();
remoteControls.Add(new RemoteControl_Electrolux());
remoteControls.Add(new RemoteControl_Samsung());
remoteControls.Add(new RemoteControl_LG());
foreach (RemoteControl control in remoteControls)
{
control.TurnOnOff();
}
what if I use it as a weapon and throw it at someone? Does that make it polymorphic
no, it does not make it polymorphic as remote control does not have behaviour of a weapon. And this is a violation of Liskov substitution principle of SOLID principles.