1. 1
    Since programming is complex, it’s easy to make mistakes. For example, maybe we try to use an int like a string. This would cause a type error. C# has rules built-in that check for those mistakes b…
  2. 2
    For this lesson we will be designing a new set of transportation machines that satisfy the requirements of BOTH car designers and the highway patrol. First the highway patrol tells us: “Every autom…
  3. 3
    Our interface is complete! Pretty easy, right? As we design our automobile-like classes, we’ll need to implement this IAutomobile interface. In C#, we must first clearly announce that a class impl…
  4. 4
    The Sedan needs to satisfy more than the highway patrol’s rules (the IAutomobile interface). The car designers have asked that sedans are built and move in certain ways — it must have constru…
  5. 5
    We’ve completed a Sedan class that satisfies both car designers and highway patrol: it can be constructed and change speed, and it implements the IAutomobile interface. But sedans aren’t the only…
  6. 6
    The car designers have asked that trucks act a bit differently from sedans. Trucks need a new property called Weight. Whenever a truck is constructed, its number of wheels will depend on its weight…
  7. 7
    Now we have a Sedan class and Truck class that implement the IAutomobile interface. Though they have some different behaviors, they both have the properties and method defined in the interface: * d…
  8. 8
    Well done! In this lesson, you: Learned that interfaces are useful to guarantee certain functionality across multiple classes Built an interface using the interface keyword * Defined properties…
  1. 1
    Duplicated code leads to errors. Say you have two classes Sedan and Truck. They’re different types, but they share a few properties and methods, like SpeedUp() and SlowDown(). If one of those membe…
  2. 2
    In inheritance, one class inherits the members of another class. The class that inherits is called a subclass or derived class. The other class is called a superclass or base class. In our…
  3. 3
    A superclass is defined just like any other class: class Vehicle { } And a subclass inherits, or “extends”, a superclass using colon syntax (:): class Sedan : Vehicle { } A class can extend a…
  4. 4
    While working on Vehicle and Sedan, you may have seen this error: Sedan.cs(11,13): error CS0200: Property or indexer ‘Vehicle.Wheels’ cannot be assigned to – it is read only Remember public an…
  5. 5
    At the start of this lesson we had duplicate code in Sedan and Truck. We know that duplicated code leads to errors, so we created a superclass Vehicle to contain that code. But one version of the …
  6. 6
    To construct a Sedan, we must first construct an instance of its superclass Vehicle. We can refer to a superclass inside a subclass with the base keyword. For example, in Sedan: base.SpeedUp(); …
  7. 7
    Say that we wanted to make one more vehicle that operates a bit differently than a sedan or truck. We want to use most of the members in Vehicle, but we need to write new versions of SpeedUp() and …
  8. 8
    Now we want to add one more method to Vehicle called Describe(). It will be different for every subclass, so there’s no point in defining a default one in Vehicle. Regardless, we want to make sure …
  9. 9
    Well done! You learned a lot very quickly, so let’s do a review: Inheritance is a way to avoid duplication across multiple classes. In inheritance, one class inherits the members of another c…

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo