Question: There is a problem with this C# code below (polymorphism is the cause). The code is throwing a runtime error. What is the problem and

There is a problem with this C# code below (polymorphism is the cause). The code is throwing a runtime error. What is the problem and can you add one line of code to correct the problem?

C# Code:

class Sensor

{

private string sensorName;

public Sensor(string _name)

{

sensorName = _name;

}

public virtual void ActionType()

{

Console.WriteLine("Sensor Detect Nothing.");

}

}

class SmokeSensor : Sensor

{

private string type;

public SmokeSensor(string _type, string _name) : base(_name)

{

type = _type;

}

public override void ActionType ()

{

Console.WriteLine("Somke Sensor Detect Smoke.");

}

}

class Program

{

static void Main(string[] args)

{

Sensor super1, super2;

SmokeSensor sub1, sub2;

super1 = new Sensor("Sensor");

sub1 = new SmokeSensor("Smoke", "Smoke Sensor");

super2 = super1;

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ \\Tip: this is where the code should go.

sub2 = (SmokeSensor)super1;

}

}

Remember to also describe what the problem is.

Thank you in advance.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!