Skip to content

Inheritance

Sann Lynn Htun edited this page Nov 19, 2024 · 2 revisions

C# Inheritance

Inheritance in C# allows one class to acquire the properties, methods, and behaviors of another class. This promotes code reusability, extensibility, and better organization of code.


Key Features of Inheritance

  1. Base Class (Parent)

    • The class being inherited from.
    • Defines shared properties and methods.
  2. Derived Class (Child)

    • The class inheriting from the base class.
    • Can add or override functionality.
  3. Access Modifiers

    • public: Accessible to derived classes and outside the class.
    • protected: Accessible only within the class and derived classes.
    • private: Not accessible in derived classes.
  4. Syntax

    • Use the colon (:) to define inheritance.

Types of Inheritance

1. Single Inheritance

  • A single derived class inherits from one base class.

Example:

class Animal {
    public void Eat() {
        Console.WriteLine("This animal eats food.");
    }
}

class Dog : Animal {
    public void Bark() {
        Console.WriteLine("The dog barks.");
    }
}

// Usage
Dog dog = new Dog();
dog.Eat();  // Output: This animal eats food.
dog.Bark(); // Output: The dog barks.

2. Hierarchical Inheritance

  • Multiple derived classes inherit from a single base class.

Example:

class Animal {
    public void Sleep() {
        Console.WriteLine("This animal sleeps.");
    }
}

class Dog : Animal {
    public void Bark() {
        Console.WriteLine("The dog barks.");
    }
}

class Cat : Animal {
    public void Meow() {
        Console.WriteLine("The cat meows.");
    }
}

// Usage
Dog dog = new Dog();
dog.Sleep(); // Output: This animal sleeps.
dog.Bark();  // Output: The dog barks.

Cat cat = new Cat();
cat.Sleep(); // Output: This animal sleeps.
cat.Meow();  // Output: The cat meows.

3. Multilevel Inheritance

  • A derived class inherits from another derived class.

Example:

class Animal {
    public void Breathe() {
        Console.WriteLine("This animal breathes.");
    }
}

class Mammal : Animal {
    public void Walk() {
        Console.WriteLine("This mammal walks.");
    }
}

class Human : Mammal {
    public void Speak() {
        Console.WriteLine("Humans can speak.");
    }
}

// Usage
Human human = new Human();
human.Breathe(); // Output: This animal breathes.
human.Walk();    // Output: This mammal walks.
human.Speak();   // Output: Humans can speak.

4. Multiple Inheritance

  • Not supported directly in C# to avoid ambiguity.
  • Achieved using interfaces.

Example (using interfaces):

interface IAnimal {
    void Eat();
}

interface IWalker {
    void Walk();
}

class Human : IAnimal, IWalker {
    public void Eat() {
        Console.WriteLine("Humans eat food.");
    }

    public void Walk() {
        Console.WriteLine("Humans walk upright.");
    }
}

// Usage
Human human = new Human();
human.Eat();  // Output: Humans eat food.
human.Walk(); // Output: Humans walk upright.

Inheritance with Access Modifiers

Modifier Base Class Accessibility Derived Class Accessibility
public Accessible everywhere. Accessible everywhere.
protected Not accessible outside. Accessible in derived classes.
private Only accessible within class. Not accessible in derived class.

Overriding and Virtual Methods

1. Virtual and Override Methods

  • Virtual methods in the base class can be overridden in the derived class.

Example:

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

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

// Usage
Dog dog = new Dog();
dog.Sound(); // Output: The dog barks.

2. Sealed Methods

  • Prevent further overriding in derived classes.

Example:

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

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

Abstraction

Abstraction in C# allows you to hide the implementation details and show only the essential features of an object. It helps in reducing complexity and allows you to focus on what an object does instead of how it does it.

Abstract Classes

  • Abstract Class: Cannot be instantiated and may contain abstract methods that must be implemented by derived classes.
  • Abstract Method: A method without a body that must be overridden in derived classes.

Example:

abstract class Animal {
    public abstract void Sound();
    
    public void Sleep() {
        Console.WriteLine("This animal sleeps.");
    }
}

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

// Usage
Dog dog = new Dog();
dog.Sound(); // Output: The dog barks.
dog.Sleep(); // Output: This animal sleeps.

Interfaces

  • Interface: Defines a contract that implementing classes must fulfill. Interfaces can contain methods, properties, events, and indexers but no implementation.

Example:

interface IAnimal {
    void Sound();
}

class Dog : IAnimal {
    public void Sound() {
        Console.WriteLine("The dog barks.");
    }
}

// Usage
Dog dog = new Dog();
dog.Sound(); // Output: The dog barks.

Summary

  • Abstraction: Hides complex implementation details and shows only the necessary features.
  • Abstract Classes: Define methods to be implemented by derived classes.
  • Interfaces: Define a contract for classes to implement.

Abstraction helps in managing complexity and focusing on the relevant aspects of an object, making your code more modular and easier to maintain. Combining inheritance and abstraction can lead to highly reusable and flexible code structures.

C# Basics Wiki

Core Concepts

Object-Oriented Programming (OOP)

Advanced Topics

Miscellaneous

Tools and Resources

Clone this wiki locally