Hello Everyone,
The question was asked me in interview.
Explain the OOP with Traffic Signal (Real Time Example) answer should cover the Abstraction,Inheritance,Polymorphism,Encapsulation with C# code.
Thanks in advance.
OOP real time example - Traffic Signal
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.


Rajeesh MenothRank #118 · 18.4K pointsPosted Jan 9, 2016, 6:31 AM EST
public abstract class TrafficLight { public bool IsTrafficAllowed { get; protected set;} public abstract void AllowTraffic(); public abstract void BlockTraffic(); } public class CarsTrafficLight : TrafficLight { public bool IsGreenLightOn {get; private set;} public bool IsOrangeLightOn {get; private set;} public bool IsRedLightOn {get; private set;} public override void AllowTraffic() { IsGreenLightOn = true; IsOrangeLightOn = false; IsRedLightOn = false; base.IsTrafficAllowed = true; } public override void BlockTraffic() { IsOrangeLightOn = true; Thread.Sleep(1000); IsGreenLightOn = false; IsRedLightOn = true base.IsTrafficAllowed = false; } } public class PedestriansTrafficLight : TrafficLight { //Does not have orange light, and a sound is played in some countries }