Object-Oriented Programming: Inheritance

Inheritance is a pillar of Object-Oriented programming that aims to re-use code written within an application. Inheritance assists in following the DRY principle (Don’t-Repeat-Yourself). Inheritance allows classes to inherit from a parent class. If we discuss this in the context of humans, children can inherit traits from their biological parents such as their eyes, ears, and body type and size.

Before we actually show a use case of how Inheritance can reduce redundant code, let’s take a look at a scenario we have listed below:

class Dog {

   public void eat()
   {
      Console.WriteLine("Eating.."); 
   }
   public void sleep()
   {
      Console.WriteLine("Sleeping..");
   }
    public void bark()
    {
       Console.WriteLine("The Dog is barking..."); 
    }

}

We’ve created a dog class with functions where the dog can eat(), sleep(), and bark(), Now lets create another class similar to the Dog class, except this time we’ll call it a Cat:

class Cat
{
   public void eat()
   {
      Console.WriteLine("Eating.."); 
   }
   public void sleep()
   {
      Console.WriteLine("Sleeping..");
   }
   public void meow()
   {
     Console.WriteLine("The Cat is meowing... ");
   }

}

Here are the results if we run the application below:

 class Program
  { 
    static void Main(string[] args)
    {
      // Create a dog object
      Dog dog = new Dog();
      // Create a cat object
      Cat cat = new Cat(); 

      // Call the methods in the Dog class
      dog.eat();
      dog.sleep();
      dog.bark();

      Console.WriteLine(); // New Line to format Output

      // Call the Methods in the Cat class
      cat.eat();
      cat.sleep();
      cat.meow();
    }
  }

If we run the application below, we have the following result:

Eating..
Sleeping..
The Dog is barking...

Eating..
Sleeping..
The Cat is meowing...

But what if I wanted to create another animal lets say a class called Bird, I would have to define the Eat() and Sleep() methods over again within the Bird class which seems redundant, we also violate the DRY principle of (Don’t-Repeat-Yourself) due to the fact that the two methods of Eat() and Sleep() are replicated within the two classes we created which have the same operations.

Furthermore, every time we create another animal class we would have to create the Eat() and Sleep() methods again within that class. I hope you understand that idea and the issue we’re attempting to solve at this point, if not maybe the next section would clear things up a little.

Inheritance To The Rescue!

Earlier we discussed the attributes of a human, and how children can inherit physical-biological traits from their parents. This similar approach can be applied to the pillar of Inheritance in Object-Oriented programming. Since we know that all animals have similar traits of eat() and sleep() we can extract these methods out into a parent class:

  • Eat()
  • Sleep()

Below I’ve created a parent class with the two attributes that all animals have in common these are all traits that animals can share among each other:

class Animal 
{
   public void eat()
   {
      Console.WriteLine("Eating.."); 
   }
   public void sleep()
   {
      Console.WriteLine("Sleeping..");
   }

}

Next we can extract the behaviors from the dog and inherit the behaviors from the Animal class, this is usually called the child class:

class Dog : Animal
{
    public void bark()
    {
       Console.WriteLine("The Dog is barking..."); 
    }

}

Likewise, we can do the same for the cat class:

class Cat : Animal
{
   public void meow()
   {
     Console.WriteLine("The Cat is meowing... ");
   }

}


In our main method, we can create a Cat and a Dog Object, and hopefully this will paint a some-what clear picture of the idea behind inheritance:

 class Program
  { 
    static void Main(string[] args)
    {
      // Create a dog object
      Dog dog = new Dog();
      Cat cat = new Cat(); 

      // Call the Animal Methods Located within the Animal Class
      dog.eat();
      dog.sleep();

      // Call the Method defined explicitly in the Dog Class
      dog.bark();

      Console.WriteLine(); // New Line to format Output

      // Call the Animal Methods Located within the Animal Class
      cat.eat();
      cat.sleep();

      // Call the Method defined explicitly in the Dog Class
      cat.meow();
    }
  }

Here we can see the output similar to our previous output:

Eating..
Sleeping..
The Dog is barking...

Eating..
Sleeping..
The Cat is meowing...

I hope this summary gives you a brief understanding of Inheritance, and how this may help in computer programming!