Module 3 - Chapter 2: Inheritance in C#

Inheritance is a fundamental concept of object-oriented programming (OOP) that allows you to create a new class that reuses, extends, and modifies the behavior that is defined in another class. This concept is one of the main pillars of OOP because it allows for the creation of hierarchical classifications. Using inheritance, you can create a general class that defines traits common to a set of related items. This class can then be inherited by other, more specific classes, each adding those features that are unique to it. In C#, inheritance is achieved by using the : symbol. This chapter will explore how inheritance works in C# and how it can be used to create well-organized and reusable code.

Mar 28, 2024

Inheritance in C#

Inheritance is a fundamental concept of object-oriented programming (OOP) that allows you to create a new class that reuses, extends, and modifies the behavior that is defined in another class. This concept is one of the main pillars of OOP because it allows for the creation of hierarchical classifications. Using inheritance, you can create a general class that defines traits common to a set of related items. This class can then be inherited by other, more specific classes, each adding those features that are unique to it. In C#, inheritance is achieved by using the : symbol. This chapter will explore how inheritance works in C# and how it can be used to create well-organized and reusable code.

Base and Derived Classes

In C#, the class from which other classes derive fundamental behavior is called the base class. Classes that inherit from the base class are called derived classes.

Defining a Base Class

public class Animal { public string Name { get; set; } public void Eat() { Console.WriteLine("Eating..."); } }

Defining a Derived Class

public class Dog : Animal // Inherits from Animal { public void Bark() { Console.WriteLine("Woof!"); } }
In this example, Dog inherits from Animal, meaning it can use the Eat method defined in Animal, as well as its own Bark method.

Access Modifiers

Access modifiers are keywords used to specify the accessibility of a class and its members. The most common access modifiers include public, private, protected, and internal.
  • Public: The member can be accessed from any code in the program.
  • Private: The member can only be accessed from within the same class.
  • Protected: The member can be accessed within its class and by derived class instances.
  • Internal: The member can be accessed by any code in the same assembly, but not from another assembly.

Method Overriding and Hiding

Derived classes can override or hide inherited members:

Method Overriding

To override a method in the base class, the method must be marked with the virtual keyword in the base class, and the override keyword must be used in the derived class.
public class Bird : Animal { public override void Eat() { Console.WriteLine("Eating seeds..."); } }

Method Hiding

If the derived class declares a member with the same name as a member in the base class, the base class member is hidden. This is done using the new keyword.
public class Cat : Animal { public new void Eat() { Console.WriteLine("Eating fish..."); } }

The base Keyword

The base keyword is used to access members of the base class from within a derived class.
public class Horse : Animal { public override void Eat() { base.Eat(); // Call the base class Eat method Console.WriteLine("Eating hay..."); } }

Summary

Inheritance allows for a more natural and logical organization of your code, enabling you to build complex behaviors by combining simple, reusable components. By understanding and implementing inheritance in C#, you can create a hierarchical classification system that simplifies code management and promotes reuse. This concept, combined with access modifiers, method overriding, and hiding, provides a powerful toolkit for any C# developer looking to create flexible and maintainable code.