Controlling inheritance with abstract
Now that we’ve covered some refactoring patterns around inheritance, let’s look at using abstract classes and other C# features to restrict our classes and ensure they’re used appropriately.
Communicating intent with abstract
One quirk about our current design is that it is possible to instantiate a new instance of FlightInfoBase
simply by writing the following code:
FlightInfoBase flight = new FlightInfoBase();
While it might not make sense to you – for a new flight to exist that isn’t explicitly a passenger or freight flight, because the FlightInfoBase
class is not marked as abstract – there’s nothing preventing anyone from instantiating it.
To mark a class as abstract, add the abstract
keyword to its signature:
FlightInfoBase.cs
public abstract class FlightInfoBase : IFlightInfo { public Airport ArrivalLocation { get; set; } public DateTime ArrivalTime...