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?
CodePudding user response:
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
CodePudding user response:
Yes, it could. Think in a remote controller compatible with Air Coinditioners of the same brand. You can raise or lower the temperature of several models, but you can't for example enable ECO System for all models. You have a base behavior sharing an interface and you have concrete remotes specialized with a very concrete behavior.
And yes, throw a remote as a weapon could be polymorphic as you can think of it as an object. All objects in your house could be throwed against someone. All classes derive from object class (simple inheritance), so all objects could share the object base behavior.