In this part of my C# tutorial we’ll be covering Abstract Classes, Virtual Methods, Abstract Methods, Override, Base Classes, Is, As, Casting and more about Polymorphism.
Thank you so much for joining me. I greatly appreciate it! All of the code along with a transcript / cheat sheet can be found after the video below to help you learn.
If you like videos like this consider donating a $1 on Patreon, but if you can’t that is fine. I’m just happy that you watched the video.
Code & Transcript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
// ---------- Shape.cs ---------- using System; // Very often you want to define a // class that you don't want to be // instantiated. In that case an // Abstract class may be the way to // go. namespace CSharpTutA.cs { abstract class Shape { public string Name { get; set; } // You can define non-abstract // methods in an abstract class public virtual void GetInfo() { Console.WriteLine($"This is a {Name}"); } // We want subclasses to override // this method so mark it as abstract // You can only make abstract methods // in abstract classes public abstract double Area(); } } // ---------- Circle.cs ---------- using System; namespace CSharpTutA.cs { class Circle : Shape { public double Radius { get; set; } public Circle(double radius) { Name = "Circle"; Radius = radius; } public override double Area() { return Math.PI * (Math.Pow(Radius, 2.0)); } // You can replace the method using override public override void GetInfo() { // Execute the base version base.GetInfo(); Console.WriteLine($"It has a Radius of {Radius}"); } } } // ---------- Rectangle.cs ---------- using System; namespace CSharpTutA.cs { class Rectangle : Shape { public double Length { get; set; } public double Width { get; set; } public Rectangle(double length, double width) { Name = "Rectangle"; Length = length; Width = width; } public override double Area() { return Length * Width; } // You can replace the method using override public override void GetInfo() { // Execute the base version base.GetInfo(); Console.WriteLine($"It has a Length of {Length} and Width of {Width}"); } } } // ---------- Program.cs ---------- using System; // This time we cover Abstract Classes, Abstract // Methods, Base Classes, Is, As, Casting and // more about Polymorphism namespace CSharpTutA.cs { class Program { static void Main(string[] args) { // We can store our shapes in // a Shape array as long as it // contains subclasses of Shape Shape[] shapes = {new Circle(5), new Rectangle(4,5)}; // Cycle through shapes and print // the area foreach(Shape s in shapes) { // Call the overidden method s.GetInfo(); Console.WriteLine("{0} Area : {1:f2}", s.Name, s.Area()); // You can use as to check if an // object is of a specific type Circle testCirc = s as Circle; if(testCirc == null) { Console.WriteLine("This isn't a Circle"); } // You can use is to check the data // type if(s is Circle) { Console.WriteLine("This isn't a Rectangle"); } Console.WriteLine(); } // You can store any class as a base // class and call the subclass methods // even if they don't exist in the base // class by casting object circ1 = new Circle(4); Circle circ2 = (Circle)circ1; Console.WriteLine("The {0} Area is {1:f2}", circ2.Name, circ2.Area()); Console.ReadLine(); } } } |
Leave a Reply