C# Base and Derived Class


In C#, a base class and a derived class are fundamental concepts of inheritance, a core principle of object-oriented programming (OOP). Here's a detailed explanation of both concepts, their roles, and how they interact with each other.

Base Class

A base class (also known as a parent class or superclass) is a class that provides common properties, methods, and behaviors that can be inherited by derived classes. The base class defines a general structure that derived classes can build upon or modify.

Characteristics of Base Class:

  • It can contain fields, properties, methods, and events.
  • It can have access modifiers (public, protected, private, internal) that determine its accessibility.
  • It may contain virtual or abstract methods that can be overridden by derived classes.

Example of Base Class:

public class Animal // Base class { public string Name { get; set; } public void Eat() { Console.WriteLine($"{Name} is eating."); } public virtual void Speak() // Virtual method { Console.WriteLine($"{Name} makes a sound."); } }

Derived Class

A derived class (also known as a child class or subclass) is a class that inherits from a base class. It can access the public and protected members of the base class and can add new members or override existing ones to provide specialized behavior.

Characteristics of Derived Class:

  • It inherits members (fields, properties, methods) from the base class.
  • It can override virtual or abstract methods from the base class to provide specific implementations.
  • It can introduce new members that are specific to the derived class.

Example of Derived Class:

public class Dog : Animal // Derived class { public void Fetch() { Console.WriteLine($"{Name} is fetching the ball."); } public override void Speak() // Overriding base class method { Console.WriteLine($"{Name} barks."); } } // Usage Dog dog = new Dog(); dog.Name = "Buddy"; dog.Eat(); // Outputs: Buddy is eating. dog.Speak(); // Outputs: Buddy barks. dog.Fetch(); // Outputs: Buddy is fetching the ball.

Key Points

  1. Inheritance Relationship:

    • A derived class inherits all non-private members (fields, properties, methods) from the base class, promoting code reuse.
    • A derived class can access public and protected members of the base class, but not private members.
  2. Method Overriding:

    • A derived class can override methods marked as virtual in the base class, allowing it to provide its own implementation.
    • If a method in the base class is marked as abstract, the derived class must provide an implementation.
  3. Constructors:

    • When creating an instance of a derived class, the constructor of the base class is called first (if defined). You can explicitly call a base class constructor using the base keyword.
  4. Polymorphism:

    • Base class references can refer to derived class objects. This allows for polymorphic behavior, where a single reference can invoke methods that are overridden in the derived class.

Example of Base and Derived Classes in Action

public class Animal // Base class { public string Name { get; set; } public virtual void Speak() { Console.WriteLine($"{Name} makes a sound."); } } public class Cat : Animal // Derived class { public override void Speak() // Overriding method { Console.WriteLine($"{Name} meows."); } } public class Program { public static void Main() { Animal myAnimal = new Cat(); // Base class reference to a derived class object myAnimal.Name = "Whiskers"; myAnimal.Speak(); // Outputs: Whiskers meows. } }

Summary

  • Base Class: Provides a general definition and functionality that can be shared among derived classes. It can contain properties, methods, and can define behavior through virtual or abstract methods.
  • Derived Class: Inherits from a base class and can extend or modify its behavior. It can override base class methods and add new members.

Understanding the concepts of base and derived classes in C# is essential for leveraging the power of inheritance, promoting code reuse, and enabling polymorphic behavior in object-oriented programming.