0% found this document useful (0 votes)
2 views

Lab1.8 Multilevel inheritance

d

Uploaded by

karantestingdemo
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab1.8 Multilevel inheritance

d

Uploaded by

karantestingdemo
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EmployeeDynamicPoly
{

using System;

public class Vehicle


{
public string Brand { get; set; }
public int Year { get; set; }

public Vehicle(string brand, int year)


{
Brand = brand;
Year = year;
}

public virtual void DisplayInfo()


{
Console.WriteLine($"[Level 0] Vehicle - Brand: {Brand}, Year: {Year}");
}
}

public class Car : Vehicle


{
public int NumberOfDoors { get; set; }

public Car(string brand, int year, int doors) : base(brand, year)


{
NumberOfDoors = doors;
}

public override void DisplayInfo()


{
base.DisplayInfo();
Console.WriteLine($"[Level 1] Car - Number of Doors: {NumberOfDoors}");
}
}

public class ElectricCar : Car


{
public int BatteryCapacity { get; set; }

public ElectricCar(string brand, int year, int doors, int batteryCapacity)


: base(brand, year, doors)
{
BatteryCapacity = batteryCapacity;
}

public override void DisplayInfo()


{
base.DisplayInfo();
Console.WriteLine($"[Level 2] Electric Car - Battery Capacity: {BatteryCapacity} kWh");
}
}

public class Motorcycle : Vehicle


{
public bool HasSidecar { get; set; }

public Motorcycle(string brand, int year, bool hasSidecar) : base(brand, year)


{
HasSidecar = hasSidecar;
}
public override void DisplayInfo()
{
base.DisplayInfo();
Console.WriteLine($"[Level 1] Motorcycle - Has Sidecar: {HasSidecar}");
}
}

class Program
{
static void Main(string[] args)
{
Console.WriteLine("Demonstrating Multilevel and Hierarchical Inheritance:");
Console.WriteLine("---------------------------------------------------");

Vehicle genericVehicle = new Vehicle("Generic", 2022);


Car sedan = new Car("Toyota", 2023, 4);
ElectricCar tesla = new ElectricCar("Tesla", 2024, 4, 75);
Motorcycle harley = new Motorcycle("Harley-Davidson", 2023, false);

Console.WriteLine("Generic Vehicle:");
genericVehicle.DisplayInfo();
Console.WriteLine();

Console.WriteLine("Car (Sedan):");
sedan.DisplayInfo();
Console.WriteLine();

Console.WriteLine("Electric Car:");
tesla.DisplayInfo();
Console.WriteLine();

Console.WriteLine("Motorcycle:");
harley.DisplayInfo();
}
}
}

program demonstrates:

1. Multilevel Inheritance:
○ Vehicle is the base class
○ Car inherits from Vehicle
○ ElectricCar inherits from Car
2. This forms a multilevel inheritance chain: Vehicle -> Car -> ElectricCar
3. Hierarchical Inheritance:
○ Both Car and Motorcycle inherit directly from Vehicle
4. This forms a hierarchical structure where multiple classes inherit from a single base
class.

The DisplayInfo() method is overridden in each class to show how each level adds its own
specific information while also calling the base class method.

You might also like