Advertisement
karlakmkj

Inheritance - abstract

Nov 25th, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.18 KB | None | 0 0
  1. //In Bicycle.cs file
  2. using System;
  3.  
  4. namespace LearnInheritance
  5. {
  6.   class Bicycle : Vehicle
  7.   {
  8.  
  9.     public Bicycle(double speed) : base(speed)
  10.     {
  11.       Wheels = 2;
  12.     }
  13.  
  14.     public override void SpeedUp()
  15.     {
  16.       Speed += 5;
  17.      
  18.       if (Speed > 15)
  19.       {
  20.         Speed = 15;
  21.       }
  22.     }
  23.  
  24.     public override void SlowDown()
  25.     {
  26.       Speed -= 5;
  27.  
  28.       if (Speed < 0)
  29.       {
  30.         Speed = 0;
  31.       }
  32.     }
  33.    
  34.     //Overriden string method -- use return since there is no void
  35.     public override string Describe()
  36.     {
  37.       return $"This Bicycle is moving on {Wheels} wheels at {Speed} km/h, with license plate {LicensePlate}.";
  38.     }
  39.  
  40.   }
  41. }
  42. ============================================================================================================================
  43. //In Vehicle.cs file
  44. using System;
  45.  
  46. namespace LearnInheritance
  47. {
  48.   abstract class Vehicle  //label class as abstract so long as it has one abstract method
  49.   {
  50.     public string LicensePlate
  51.     { get; private set; }
  52.  
  53.     public double Speed
  54.     { get; protected set; }
  55.  
  56.     public int Wheels
  57.     { get; protected set; }
  58.  
  59.     public Vehicle(double speed)
  60.     {
  61.       Speed = speed;
  62.       LicensePlate = Tools.GenerateLicensePlate();
  63.     }
  64.  
  65.     public virtual void SpeedUp()
  66.     {
  67.       Speed += 5;
  68.     }
  69.  
  70.     public virtual void SlowDown()
  71.     {
  72.       Speed -= 5;
  73.     }
  74.    
  75.     public void Honk()
  76.     {
  77.       Console.WriteLine("HONK!");
  78.     }
  79.  
  80.     //abstract method -- has no defailt functionality to inherit like virtual above
  81.     public abstract string Describe();
  82.  
  83.   }
  84. }
  85. ============================================================================================================================
  86. //In Program.cs file
  87. using System;
  88.  
  89. namespace LearnInheritance
  90. {
  91.   class Program
  92.   {
  93.     static void Main(string[] args)
  94.     {
  95.       Sedan s = new Sedan(60);
  96.       s.SpeedUp();
  97.       Console.WriteLine(s.Describe());
  98.      
  99.       Truck t = new Truck(45, 500);
  100.       t.SpeedUp();
  101.       Console.WriteLine(t.Describe());
  102.      
  103.       Bicycle b = new Bicycle(10);
  104.       b.SpeedUp();
  105.       Console.WriteLine(b.Describe());
  106.     }
  107.   }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement