Creating the classes
Start in the usual way by creating a C# console app and clearing out all of the existing template code. Next, we type the first line of our code:
using static System.Console;
As usual, we need this so that we can use WriteLine
. Next, create an abstract
class to work with. For this, type the following:
abstract class Shape { }
This class here is needed so that we can switch between objects of the type Shape
. People sometimes do something similar with interfaces. Well, I'm using an abstract
class for the following reason: classes are used to essentially express the "is a" type of relationship, while interfaces are used to express the "can be used as" type of relationship. Let's take a look and see why I chose to go with a class. Underneath the closing brace of our Shape
class, start a new line and type the following:
class Rectangle:Shape { }
We've created a new Rectangle
class with a parent class of Shape
. Let's examine our logic and see why I chose to use a class rather...