Inheritance - potatoscript/csharp GitHub Wiki

🥔 Inheritance in C# 🥔

🥔 What is Inheritance? 🥔

Inheritance is one of the key concepts in object-oriented programming (OOP). It allows a class to inherit properties and methods from another class. This is similar to how children inherit traits from their parents, like eye color or hair type.

In programming, a child class (also called a derived class) inherits the properties and methods of a parent class (also called a base class). This helps us reuse code and avoid writing the same thing again and again.

🥔 Why Do We Use Inheritance? 🥔

  • Code Reusability: Inheritance helps us reuse the code that already exists in the parent class, making our programs shorter and easier to maintain.
  • Organized Code: By inheriting from a parent class, we can keep related classes together, making the code easier to understand and extend.

🥔 How to Use Inheritance in C#? 🥔

In C#, we use the : (colon) symbol to indicate that one class is inheriting from another class.

🥔 Example of Inheritance 🥔

Let's say we have a class called Animal. We can create a Dog class that inherits the properties and methods of Animal.

// Parent class
public class Animal
{
    public string Name { get; set; }
    public int Age { get; set; }

    // Method in the parent class
    public void Eat()
    {
        Console.WriteLine("This animal is eating.");
    }
}

// Child class
public class Dog : Animal
{
    public string Breed { get; set; }

    // Additional method in the child class
    public void Bark()
    {
        Console.WriteLine("Woof! Woof!");
    }
}

🥔 Explanation 🥔

  • Parent Class (Animal): The Animal class has two properties: Name and Age, and a method Eat.
  • Child Class (Dog): The Dog class inherits from Animal using the : Animal syntax. This means Dog automatically has the properties Name and Age, as well as the Eat method. We also added a new property Breed and a new method Bark to the Dog class.

🥔 Using Inherited Classes 🥔

Now, let's create objects of both the Animal and Dog classes and see how inheritance works.

class Program
{
    static void Main()
    {
        // Create an object of the parent class
        Animal animal = new Animal();
        animal.Name = "Elephant";
        animal.Age = 10;
        Console.WriteLine($"{animal.Name} is {animal.Age} years old.");
        animal.Eat();  // Call the method from the parent class

        // Create an object of the child class
        Dog dog = new Dog();
        dog.Name = "Buddy";
        dog.Age = 5;
        dog.Breed = "Golden Retriever";
        Console.WriteLine($"{dog.Name} is {dog.Age} years old and is a {dog.Breed}.");
        dog.Eat();  // Call the inherited method from the parent class
        dog.Bark();  // Call the method from the child class
    }
}

🥔 Explanation 🥔

  • Inheritance: The dog object is of type Dog, but it can still use the Eat method from the Animal class because Dog inherits from Animal.
  • Calling Methods: We can call methods from both the parent and child class, like Eat from Animal and Bark from Dog.

🥔 Overriding Methods (Method Overriding) 🥔

In some cases, we may want to change the behavior of a method that was inherited from the parent class. This is called method overriding. We can do this by using the override keyword.

For example, if we want the Eat method in the Dog class to print something different, we can override it:

public class Animal
{
    public string Name { get; set; }
    public int Age { get; set; }

    public virtual void Eat()
    {
        Console.WriteLine("This animal is eating.");
    }
}

public class Dog : Animal
{
    public string Breed { get; set; }

    // Override the Eat method
    public override void Eat()
    {
        Console.WriteLine("The dog is eating its food.");
    }

    public void Bark()
    {
        Console.WriteLine("Woof! Woof!");
    }
}

🥔 Explanation 🥔

  • The Eat method in the Animal class is marked as virtual to indicate that it can be overridden in derived classes.
  • The Dog class overrides the Eat method and provides its own implementation, which is different from the Animal class version.

🥔 The base Keyword 🥔

Sometimes, you may want to call a method from the parent class inside the overridden method of the child class. You can do this by using the base keyword.

For example, let's call the Eat method from the parent class Animal inside the Eat method of Dog:

public class Dog : Animal
{
    public string Breed { get; set; }

    public override void Eat()
    {
        // Call the Eat method from the parent class
        base.Eat();
        Console.WriteLine("The dog is eating its food.");
    }

    public void Bark()
    {
        Console.WriteLine("Woof! Woof!");
    }
}

🥔 Explanation 🥔

  • base.Eat() calls the Eat method from the Animal class before printing the message specific to the Dog class.