Lesson Sunday

In the last lesson, we learned how to add interfaces that extend classes. In this lesson, we'll learn more about the concept of inheritance as well as another way to pass functionality to other classes: abstract classes.

Inheritance is a key part of object-oriented programming. Inheritance is simply the process of passing on functionality from a parent class to a child class. C# is single inheritance, which means that a class can only inherit from a single other class. In turn, that class can inherit from yet another class. A subclass, or child class, ultimately inherits the functionality of each of those parent classes. For example, we could have a structure like this:

Organism -> Mammal -> Feline -> Cat

Cat can only inherit from one class - Feline - but it will ultimately inherit all the functionality of each of the classes above, including Mammal and Organism, because they have been passed down to Feline.

Remember that interfaces are a little different; a class can implement multiple interfaces at once.

One way to build inheritance into an application is through an abstract class. An abstract class is simply a class that is never instantiated. For example, we might have an Animal abstract class that has functionality that Bear, Chimpanzee and Duck should inherit. However, we won't be able to create an instance of Animal and tell it to Walk(). After all, we don't know what kind of animal it is yet and the animals listed above walk differently. In other words, an abstract class holds elements that are universal to its children and nothing else. It exists simply for the purpose of passing on functionality to other classes. There is no limit to the number of classes that can inherit from a class. Bear can only have one parent class but Animal can have many child classes.

Implementing an Abstract Class


It's easy to create an abstract class:

abstract class Animal
{
    ...
}

All we need is the abstract keyword. Once we do this, we can no longer instantiate a single Animal. If we tried to do something like this:

    Animal animal = new Animal();

We would receive the following error:

error CS0144: Cannot create an instance of the abstract class or interface 'Animal'

However, we can still declare instance methods. These methods will be available in any child classes. For instance, we can declare an abstract method:

abstract class Animal
{
  public abstract string Walk();
}

Note that we declare the method is abstract. This method cannot include any code. It's just a placeholder for the method in child classes. In this way, an abstract method is similar to a method declared in an interface.

Abstract classes may contain abstract members (methods or properties). When they do, child classes that are inheriting from the abstract class must override the abstract member. For instance, we'd need to include a Walk() method in the Chimpanzee class that overrides the Walk() method in the parent class.

We can also include methods in an abstract class that don't use the abstract keyword. These methods can include code. For instance, our Walk() method could look like this instead:

abstract class Animal
{
  public string Walk()
  {
    return "The animal is walking";
  };
}

Because this method isn't abstract, we can add code to it. Child classes can then override this code. For instance, our Chimpanzee class might include the following code:

class Chimpanzee : Animal
{
  public string Walk()
  {
    return "The chimpanzee is running and leaping.";
  };
}

If we were to instantiate a Chimpanzee and call the Walk() method on it, our application would return "The chimpanzee is running and leaping." The method in the child class overrides the method in the parent class.

Difference Between Abstract Classes and Interfaces


It may seem like abstract classes and interfaces are very similar. Neither abstract classes nor interfaces can have instances. Instead, other classes must extend or inherit their behavior. However, there are a number of key differences between the two. Here are a few:

  • A class can only inherit from one abstract class. However, a class can be extended by multiple interfaces.
  • An abstract class provides core functionality to its child classes. Meanwhile, an interface provides peripheral functionality to the classes it extends. This difference is in part philosophical. For example, we wouldn't want instances of a Robot class to inherit Walk() functionality from an Animal abstract class because robots aren't animals. However, a Robot could inherit Walk() functionality from an IMobility interface. In this case, our application would be structured so that any kind of object with mobility includes a Walk() method whether they are Animals or Robots. We could easily argue that Walk() should belong to either a parent abstract class or to an interface. In fact, which option we choose will largely depend on the needs of our application.
  • Abstract classes are more performant than interfaces.
  • Abstract classes can have fields and constants while interfaces cannot.

There are other differences, too, but they are beyond the scope of this lesson. To learn more about abstract classes, check the Microsoft documentation.

You are not required to use an abstract class for this section's independent project. However, you are encouraged to experiment with them and add them to projects if possible.

Terminology


Inheritance: The process of passing on functionality from a parent class to a child class.

Abstract class: A class that is never instantiated.

Single inheritance: When a programming language allows a child class to inherit from only one parent class. This is in contrast to multiple inheritance.

Example


An example of an abstract class with an abstract method:

abstract class Animal
{
  public abstract string Walk();
}

Difference Between Abstract Classes and Interfaces


  • A class can only inherit from one abstract class. However, a class can be extended by multiple interfaces.
  • An abstract class provides core functionality to its child classes. Meanwhile, an interface provides peripheral functionality to the classes it extends.
  • Abstract classes are more performant than interfaces.
  • Abstract classes can have fields and constants while interfaces cannot.

Lesson 4 of 12
Last updated more than 3 months ago.