# Mastering C# Part 7.2 Object Oriented Programming

### **Inheritance in C#**

Inheritance is a fundamental concept in Object-Oriented Programming (OOP) that allows a class (derived class) to inherit properties, methods, and other members from another class (base class). It promotes **code reuse**, **extensibility**, and **polymorphism**.

---

### **Key Features of Inheritance in C#:**

1. **Base Class**: The class whose members are inherited.
    
2. **Derived Class**: The class that inherits members of the base class.
    
3. **Access Modifiers**: Determine the visibility of the base class members in the derived class (`public`, `protected`, `private`).
    
4. `is-a` Relationship: Inheritance establishes an "is-a" relationship (e.g., Dog **is a** Animal).
    
5. **Single Inheritance**: C# supports single inheritance directly, but multiple inheritance is achieved via interfaces.
    
6. **Base Keyword**: Used to access base class members or call its constructors.
    
7. **Polymorphism**: Enables overriding base class methods in the derived class.
    

---

### **Syntax**

```csharp
class BaseClass
{
    // Members of the base class
}

class DerivedClass : BaseClass
{
    // Members of the derived class
}

```

---

### **Example: Inheritance Basics**

```csharp
using System;

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

    public void Eat()
    {
        Console.WriteLine($"{Name} is eating.");
    }
}

public class Dog : Animal
{
    public void Bark()
    {
        Console.WriteLine($"{Name} is barking.");
    }
}

class Program
{
    static void Main()
    {
        Dog dog = new Dog();
        dog.Name = "Buddy";
        dog.Eat();  // Inherited from Animal
        dog.Bark(); // Defined in Dog
    }
}

```

**Output**:

```csharp
Buddy is eating.
Buddy is barking.

```

---

### **Types of Inheritance in C#**

1. **Single Inheritance**: One derived class inherits from a single base class.
    
    ```csharp
    class BaseClass { }
    class DerivedClass : BaseClass { }
    
    ```
    
2. **Hierarchical Inheritance**: Multiple derived classes inherit from a single base class.
    
    ```csharp
    class BaseClass { }
    class DerivedClass1 : BaseClass { }
    class DerivedClass2 : BaseClass { }
    
    ```
    
3. **Multilevel Inheritance**: A class inherits from a derived class, forming a chain.
    
    ```csharp
    class GrandParent { }
    class Parent : GrandParent { }
    class Child : Parent { }
    
    ```
    
4. **Multiple Inheritance (via Interfaces)**: A class implements multiple interfaces.
    
    ```csharp
    interface IInterface1 { }
    interface IInterface2 { }
    class DerivedClass : IInterface1, IInterface2 { }
    
    ```
    

---

### **Access Modifiers in Inheritance**

| **Modifier** | **Base Class** | **Derived Class (Same Assembly)** | **Derived Class (Different Assembly)** |
| --- | --- | --- | --- |
| `public` | Yes | Yes | Yes |
| `protected` | Yes | Yes | Yes |
| `private` | Yes | No | No |
| `internal` | Yes | Yes | No |
| `protected internal` | Yes | Yes | Yes |

---

### **Overriding and the** `virtual` Keyword

* **Base Class**: Mark a method as `virtual` to allow overriding.
    
* **Derived Class**: Use the `override` keyword to modify behavior.
    

```csharp
public class Animal
{
    public virtual void Sound()
    {
        Console.WriteLine("Animal makes a sound.");
    }
}

public class Dog : Animal
{
    public override void Sound()
    {
        Console.WriteLine("Dog barks.");
    }
}

class Program
{
    static void Main()
    {
        Animal animal = new Animal();
        animal.Sound();

        Animal dog = new Dog(); // Polymorphism
        dog.Sound();
    }
}

```

**Output**:

```csharp
Animal makes a sound.
Dog barks.

```

---

### **Base Keyword**

The `base` keyword is used to:

1. **Access Base Class Members**: Invoke base class properties or methods.
    
2. **Call Base Class Constructor**: Pass parameters to the base class constructor.
    

### Example:

```csharp
public class Animal
{
    public Animal(string name)
    {
        Console.WriteLine($"Animal: {name}");
    }
}

public class Dog : Animal
{
    public Dog(string name) : base(name)
    {
        Console.WriteLine($"Dog: {name}");
    }
}

class Program
{
    static void Main()
    {
        Dog dog = new Dog("Buddy");
    }
}

```

**Output**:

```csharp
Animal: Buddy
Dog: Buddy

```

---

### **Abstract Classes**

An abstract class provides a base for other classes and cannot be instantiated directly. It can include both abstract (unimplemented) and concrete (implemented) methods.

### Example:

```csharp
public abstract class Animal
{
    public abstract void Sound(); // Abstract method
    public void Eat()
    {
        Console.WriteLine("Animal is eating.");
    }
}

public class Dog : Animal
{
    public override void Sound()
    {
        Console.WriteLine("Dog barks.");
    }
}

class Program
{
    static void Main()
    {
        Dog dog = new Dog();
        dog.Eat();
        dog.Sound();
    }
}

```

---

### **Sealed Classes and Methods**

* **Sealed Class**: Cannot be inherited.
    
* **Sealed Method**: Prevents further overriding of the method in derived classes.
    

### Example:

```csharp
public sealed class FinalClass
{
    public void Display()
    {
        Console.WriteLine("This is a sealed class.");
    }
}

```

---

### **Summary**

* **Inheritance** enables code reuse and extensibility.
    
* **Access Modifiers** control the visibility of base class members.
    
* Use `virtual`, `override`, and `abstract` for polymorphism.
    
* **Sealed Classes** prevent inheritance.
    
* Use `base` to access base class members and constructors.
