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.
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.
In C#, delegates and events are foundational concepts that enable a flexible and extensible way to handle method callbacks and notifications. They play a crucial role in designing and implementing event-driven programming patterns, which are central to developing interactive applications such as graphical user interfaces, game development, and service-oriented applications. This chapter introduces delegates and events, explaining their uses, syntax, and how they enable managed event handling in C#.
Encapsulation is a fundamental concept in object-oriented programming (OOP) that involves bundling the data (attributes) and methods (functions) that operate on the data into a single unit, known as a class. It also restricts direct access to some of the object's components, which is a way of preventing accidental manipulation of data and ensuring internal data integrity. This principle of hiding the internal state and requiring all interaction to occur through an object's methods is central to C#. This chapter explains the concept of encapsulation and demonstrates how to implement it in C# through access modifiers and properties.
Functions are a cornerstone of programming, allowing developers to encapsulate code that performs a specific task into a reusable and maintainable block. In C#, functions are defined within classes or structs, and they are referred to as methods. This chapter will dive into the basics of defining and using functions in C#, covering their syntax, types, parameters, and return values. Understanding functions is crucial for both beginners and intermediate developers, as they provide the building blocks for structuring and organizing code in any application.