The Object Oriented Programming

The Object Oriented Programming (OOP) improves the scalability and reusability of the code. This comprise of class and objects. Here we can see the Object Oriented Programming with Traffic Management Example by using the C# language.

Object

Any entity that has a state and behavior is known as an object. It can be defined as the Instance of the Class.

Class

The collection of objects is known as Class. It can also be defined as Blue Print from which you can Create the Objects.

The Object Oriented Programming contains the following four concepts.

  • Encapsulation
  • Abstraction
  • Inheritance
  • Polymorphism

1. Encapsulation

Wrapping of code and data in to signle unit is known as Encapsulation. Here in the Traffic Signal Management Concept the Encapsulation is used for Managing Traffic Signal State.

Encapsulation ensures traffic signals change safely without external interference.

Example

public class TrafficSignal
{
    private string signalColor;
    
    public void ChangeSignal(string color)
    {
        if (color == "Red" || color == "Green" || color == "Yellow")
        {
            signalColor = color;
            Console.WriteLine($"Signal changed to {signalColor}");
        }
        else
        {
            Console.WriteLine("Invalid signal color.");
        }
    }
    
    public string GetSignalColor()
    {
        return signalColor;
    }
}

2. Abstraction

Here in the Traffic Signal Management Concept the Abstraction is used for Controlling Road Barriers.

Abstraction hides complex logic behind simplified interfaces.

Example

public abstract class RoadBarrier
{
    public abstract void Operate();
}

public class TollGate : RoadBarrier
{
    public override void Operate()
    {
        Console.WriteLine("Toll gate is opening for the vehicle.");
    }
}

3. Inheritance

Here in the Traffic Signal Management Concept, the Inheritance is used for Specialized Traffic Lanes.

Inheritance helps define specialized behavior for different vehicle types.

Example

public class TrafficLane
{
    public void AllowVehicles()
    {
        Console.WriteLine("General vehicles are allowed.");
    }
}

public class EmergencyLane : TrafficLane
{
    public void AllowAmbulance()
    {
        Console.WriteLine("Ambulance is given priority in the emergency lane.");
    }
}

4. Polymorphism

Here in Traffic Signal Management Concept Polymorphism is used for Dynamic Traffic Control.

Polymorphism enables dynamic behavior changes.

Example

public class TrafficController
{
    public virtual void ControlTraffic()
    {
        Console.WriteLine("Managing normal traffic flow.");
    }
}

public class SmartTrafficController : TrafficController
{
    public override void ControlTraffic()
    {
        Console.WriteLine("Adjusting traffic lights dynamically based on congestion.");
    }
}

5. Main Program Execution

Below, we can see the usage of All OOp Concept.

Example

class Program
{
    static void Main()
    {
        // Calling of Encapsulation Example.
        TrafficSignal signal = new TrafficSignal();
        signal.ChangeSignal("Green");
        Console.WriteLine($"Current Signal: {signal.GetSignalColor()}");

        // Calling of Abstraction Example
        RoadBarrier toll = new TollGate();
        toll.Operate();

        // Calling of Inheritance Example
        EmergencyLane lane = new EmergencyLane();
        lane.AllowVehicles();
        lane.AllowAmbulance();

        // Calling of Polymorphism Example
        TrafficController controller = new SmartTrafficController();
        controller.ControlTraffic();
    }
}

Sample Output for Traffic Management System.

  • Signal changed to Green
  • Current Signal: Green
  • A toll gate is opened for the vehicle.
  • General vehicles are allowed.
  • Ambulance is given priority in the emergency lane.
  • Adjusting traffic lights dynamically based on congestion.

Conclusion

  • This Traffic Management System uniquely showcases OOP principles in C#. Encapsulation secures signal data, abstraction simplifies road barriers, inheritance enables specialized lanes, and polymorphism allows adaptive traffic control.
  • Using OOP effectively leads to well-structured, scalable applications in real-world scenarios.

Up Next
    Ebook Download
    View all
    DateTime in C#
    Read by 1.3k people
    Download Now!
    Learn
    View all