Base Class and Derived Class:
The base class is the class being inherited from, and the derived class is the class inheriting from the base class.
The derived class can access public and protected members of the base class.
The base keyword is used to refer to the base class and invoke its members.
Example:
class Animal
{
public void Eat()
{
Console.WriteLine(“Animal is eating”);
}
}
class Dog : Animal
{
public void Bark()
{
Console.WriteLine(“Dog is barking”);
}
public void DoAnimalThings()
{
base.Eat(); // Calling base class method
Bark(); // Calling derived class method
}
}
Inheritance and polymorphism are powerful concepts that promote code reuse, modularity, and flexibility in object-oriented programming. They allow you to create class hierarchies, specialize behavior, and treat objects of derived classes as objects of their base classes, enhancing code organization and extensibility.