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:
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:
Key Points
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.
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.
- A derived class can override methods marked as
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.
- 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
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
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.