Untitled
unknown
plain_text
9 months ago
4.3 kB
10
Indexable
using System;
namespace OOPConceptsDemo
{
// ================================
// 🔹 ABSTRACTION
// ================================
// Abstract class only defines general structure (no detailed implementation)
public abstract class VehicleBase
{
public string Brand { get; set; }
public abstract void StartEngine();
public abstract void StopEngine();
}
// ================================
// 🔹 ENCAPSULATION
// ================================
// Data (fields) are hidden using private + accessed through public methods/properties
public class Vehicle : VehicleBase
{
private int speed; // private → cannot be accessed directly from outside
// Public property to get/set speed safely
public int Speed
{
get { return speed; }
set
{
if (value >= 0)
speed = value;
else
Console.WriteLine("Speed cannot be negative!");
}
}
// Implement abstract methods (Abstraction)
public override void StartEngine()
{
Console.WriteLine($"{Brand} engine started.");
}
public override void StopEngine()
{
Console.WriteLine($"{Brand} engine stopped.");
}
// Encapsulated behavior: only this method can change speed
public void Accelerate(int increase)
{
speed += increase;
Console.WriteLine($"{Brand} speed is now {speed} km/h");
}
}
// ================================
// 🔹 INHERITANCE
// ================================
// Child classes inherit properties/methods from Vehicle
public class Car : Vehicle
{
public int Doors { get; set; }
public void OpenDoor()
{
Console.WriteLine($"{Brand} opens {Doors} doors.");
}
}
public class Motorcycle : Vehicle
{
public bool HasSidecar { get; set; }
public void ShowType()
{
if (HasSidecar)
Console.WriteLine($"{Brand} is a sidecar motorcycle.");
else
Console.WriteLine($"{Brand} is a regular motorcycle.");
}
}
// ================================
// 🔹 POLYMORPHISM
// ================================
// Same method name → different behaviors depending on the object
public class VehiclePoly
{
public virtual void Honk()
{
Console.WriteLine("Generic vehicle sound 🚗");
}
}
public class CarPoly : VehiclePoly
{
public override void Honk()
{
Console.WriteLine("Car honks: Beep Beep!");
}
}
public class MotorcyclePoly : VehiclePoly
{
public override void Honk()
{
Console.WriteLine("Motorcycle honks: Beeeem!");
}
}
// ================================
// 🔹 MAIN PROGRAM
// ================================
class Program
{
static void Main(string[] args)
{
Console.WriteLine("=== OOP Concepts Demo in C# ===\n");
// --- Abstraction + Encapsulation ---
Vehicle myCar = new Vehicle();
myCar.Brand = "Toyota";
myCar.StartEngine();
myCar.Accelerate(50);
myCar.StopEngine();
Console.WriteLine();
// --- Inheritance ---
Car car = new Car { Brand = "Honda", Doors = 4 };
car.StartEngine();
car.OpenDoor();
car.Accelerate(60);
car.StopEngine();
Console.WriteLine();
Motorcycle motor = new Motorcycle { Brand = "Yamaha", HasSidecar = false };
motor.StartEngine();
motor.ShowType();
motor.Accelerate(80);
motor.StopEngine();
Console.WriteLine();
// --- Polymorphism ---
VehiclePoly[] vehicles = { new CarPoly(), new MotorcyclePoly() };
foreach (var v in vehicles)
{
v.Honk(); // Same method, different output depending on type
}
Console.WriteLine("\n=== End of Program ===");
}
}
}
Editor is loading...
Leave a Comment