Base Class – .NET

What Does Base Class – .NET Mean?

A base class, in the context of C#, is a class that is used to create, or derive, other classes. Classes derived from a base class are called child classes, subclasses or derived classes. A base class does not inherit from any other class and is considered parent of a derived class.

Advertisements

The base class forms the means by which inheritance is accomplished through derivation. A class derived from a base class inherits both data and behavior. For example, vehicle can be a base class from which the derived classes car and bus can be derived. Both car and bus are vehicles, and they each represent their own specializations of base class.

As in Java but unlike C++, C# does not support multiple inheritances of classes. C# differs from Java by explicitly marking a virtual modifier for all virtual members.

A base class is also known as a parent class or superclass.

Techopedia Explains Base Class – .NET

Base class helps to create a specialized class that can reuse the code that is implicitly gained from the base class (except constructors and destructors) and extend the functionality of base class by adding or overriding members relevant to derived class in derived class. In C#, events are declared in base class that can be raised from derived classes. Generic classes that are used to encapsulate operations that are not specific to a particular data type serve as base classes, providing generic behavior so as to achieve flexibility and code reusability.

The key the properties of the base class in C#:

  • Base class members (constructor, an instance method or instance property accessor) are accessed in derived class using the “base” keyword.
  • Base classes are automatically instantiated before derived classes.
  • Derived class can communicate to the base class during instantiation by calling the base class constructor with a matching parameter list.
  • Base class members can be accessed from the derived class through an explicit cast.
  • Since a base class itself can be a derived class, a class may have many base classes.
  • Members of a derived class can access the public, protected, internal and protected internal members of a base class.
  • Due to the transitive nature of inheritance, although a derived class has only one base class, it inherits the members declared in the parent of the base class.
  • By declaring a method in base class as virtual, the derived class can override that method with its own implementation. Both the overridden and overriding method and property must have the same access-level modifiers such as virtual, abstract or override.
  • When the keyword “abstract” is used for a method, it should be overridden in any nonabstract class that directly inherits from that class.
  • Abstract base classes are created using the “abstract” keyword in its declaration and are used to prevent direct initiation using the “new” keyword. They can only be used through derived classes that implement abstract methods.
  • A base class can prevent other classes from inheriting from it by declaring all the members as “sealed.”
  • Base class members can be hidden in a derived class by using the keyword “new” to indicate that the member is not intended to be an override of the base member. If “new” is not used, the compiler generates a warning.

Although the base class and interface can be used interchangeably, classes are more flexible than interfaces from a versioning perspective. Base class is preferred in most cases except the following scenarios where:

  • Several unrelated classes form the base for the derived class
  • Classes already have established base classes
  • Aggregation is not suitable or practical
Advertisements

Related Terms

Latest DevOps Terms

Related Reading

Margaret Rouse

Margaret Rouse is an award-winning technical writer and teacher known for her ability to explain complex technical subjects to a non-technical, business audience. Over the past twenty years her explanations have appeared on TechTarget websites and she's been cited as an authority in articles by the New York Times, Time Magazine, USA Today, ZDNet, PC Magazine and Discovery Magazine.Margaret's idea of a fun day is helping IT and business professionals learn to speak each other’s highly specialized languages. If you have a suggestion for a new definition or how to improve a technical explanation, please email Margaret or contact her…