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

Inheritance

The document contains C# code defining a hierarchy of vehicle classes: Vehicle, Car, Bus, and Trailer, each with properties and a method to display their information. The main program creates an array of different vehicle objects and calls their DisplayInfo method to output their details. This demonstrates the use of inheritance and polymorphism in object-oriented programming.
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

Inheritance

The document contains C# code defining a hierarchy of vehicle classes: Vehicle, Car, Bus, and Trailer, each with properties and a method to display their information. The main program creates an array of different vehicle objects and calls their DisplayInfo method to output their details. This demonstrates the use of inheritance and polymorphism in object-oriented programming.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

using System;

class Vehicle

public string Make { get; set; }

public string Model { get; set; }

public int Year { get; set; }

public virtual void DisplayInfo()

Console.WriteLine($"Make: {Make}, Model: {Model}, Year: {Year}");

class Car : Vehicle

public int NumberOfDoors { get; set; }

public override void DisplayInfo()

base.DisplayInfo();

Console.WriteLine($"Number of Doors: {NumberOfDoors}");

class Bus : Vehicle


{

public int CarryingCapacity { get; set; }

public override void DisplayInfo()

base.DisplayInfo();

Console.WriteLine($"Carrying Capacity: {CarryingCapacity} passengers");

class Trailer : Vehicle

public double Tonnage { get; set; }

public override void DisplayInfo()

base.DisplayInfo();

Console.WriteLine($"Tonnage: {Tonnage} tons");

class Program

static void Main(string[] args)

{
Vehicle[] vehicles = new Vehicle[]

new Car { Make = "Toyota", Model = "Corolla", Year = 2020, NumberOfDoors = 4 },

new Bus { Make = "Mercedes", Model = "Sprinter", Year = 2018, CarryingCapacity = 20 },

new Trailer { Make = "MAN", Model = "TGX", Year = 2021, Tonnage = 10.5 }

};

foreach (var vehicle in vehicles)

vehicle.DisplayInfo();

Console.WriteLine();

You might also like