Base Class – .NET

Why Trust Techopedia

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

Margaret Rouse
Editor

Margaret jest nagradzaną technical writerką, nauczycielką i wykładowczynią. Jest znana z tego, że potrafi w prostych słowach pzybliżyć złożone pojęcia techniczne słuchaczom ze świata biznesu. Od dwudziestu lat jej definicje pojęć z dziedziny IT są publikowane przez Que w encyklopedii terminów technologicznych, a także cytowane w artykułach ukazujących się w New York Times, w magazynie Time, USA Today, ZDNet, a także w magazynach PC i Discovery. Margaret dołączyła do zespołu Techopedii w roku 2011. Margaret lubi pomagać znaleźć wspólny język specjalistom ze świata biznesu i IT. W swojej pracy, jak sama mówi, buduje mosty między tymi dwiema domenami, w ten…