Key Concepts

Review core concepts you need to learn to master this subject

C# Interface

interface IAutomobile { string LicensePlate { get; } double Speed { get; } int Wheels { get; } } // The IAutomobile interface has three properties. Any class that implements this interface must have these three properties. public interface IAccount { void PayInFunds ( decimal amount ); bool WithdrawFunds ( decimal amount ); decimal GetBalance (); } // The IAccount interface has three methods to implement. public class CustomerAccount : IAccount { } // This CustomerAccount class is labeled with : IAccount, which means that it will implement that interface.

In C#, an interface contains definitions for a group of related functionalities that a class can implement.

Interfaces are useful because they guarantee how a class behaves. This, along with the fact that a class can implement multiple interfaces, helps organize and modularize components of software.

It is best practice to start the name of an interface with “I”.

C# Inheritance

interface IAutomobile { string LicensePlate { get; } double Speed { get; } int Wheels { get; } } // The IAutomobile interface has three properties. Any class that implements this interface must have these three properties. public interface IAccount { void PayInFunds ( decimal amount ); bool WithdrawFunds ( decimal amount ); decimal GetBalance (); } // The IAccount interface has three methods to implement. public class CustomerAccount : IAccount { } // This CustomerAccount class is labeled with : IAccount, which means that it will implement that interface.

In C#, inheritance is the process by which 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 a base class.

When you define a class that inherits from another class, the derived class implicitly gains all the members of the base class, except for its constructors and finalizers. The derived class can thereby reuse the code in the base class without having to re-implement it. In the derived class, you can add more members. In this manner, the derived class extends the functionality of the base class.

C# protected Keyword

interface IAutomobile { string LicensePlate { get; } double Speed { get; } int Wheels { get; } } // The IAutomobile interface has three properties. Any class that implements this interface must have these three properties. public interface IAccount { void PayInFunds ( decimal amount ); bool WithdrawFunds ( decimal amount ); decimal GetBalance (); } // The IAccount interface has three methods to implement. public class CustomerAccount : IAccount { } // This CustomerAccount class is labeled with : IAccount, which means that it will implement that interface.

In C#, a protected member can be accessed by the current class and any class that inherits from it. This is designated by the protected access modifier.

C# override/virtual Keywords

interface IAutomobile { string LicensePlate { get; } double Speed { get; } int Wheels { get; } } // The IAutomobile interface has three properties. Any class that implements this interface must have these three properties. public interface IAccount { void PayInFunds ( decimal amount ); bool WithdrawFunds ( decimal amount ); decimal GetBalance (); } // The IAccount interface has three methods to implement. public class CustomerAccount : IAccount { } // This CustomerAccount class is labeled with : IAccount, which means that it will implement that interface.

In C#, a derived class (subclass) can modify the behavior of an inherited method. The method in the derived class must be labeled override and the method in the base class (superclass) must be labeled virtual.

The virtual and override keywords are useful for two reasons:

  1. Since the compiler treats “regular” and virtual methods differently, they must be marked as such.
  2. This avoids the “hiding” of inherited methods, which helps developers understand the intention of the code.

C# abstract Keyword

interface IAutomobile { string LicensePlate { get; } double Speed { get; } int Wheels { get; } } // The IAutomobile interface has three properties. Any class that implements this interface must have these three properties. public interface IAccount { void PayInFunds ( decimal amount ); bool WithdrawFunds ( decimal amount ); decimal GetBalance (); } // The IAccount interface has three methods to implement. public class CustomerAccount : IAccount { } // This CustomerAccount class is labeled with : IAccount, which means that it will implement that interface.

In C#, the abstract modifier indicates that the thing being modified has a missing or incomplete implementation. It must be implemented completely by a derived, non-abstract class.

The abstract modifier can be used with classes, methods, properties, indexers, and events. Use the abstract modifier in a class declaration to indicate that a class is intended only to be a base class of other classes, not instantiated on its own.

If at least one member of a class is abstract, the containing class must also be marked abstract.

The complete implementation of an abstract member must be marked with override.

  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