Link Search Menu Expand Document

C Sharp Inheritance

In C# Inheritance is how one object inherits its parent object’s attributes and behaviors. You can reuse, extend, or alter the characteristics and functionalities described in other classes via inheritance. C# and.NET only support single inheritance. In other words, a class can only inherit from one other class. However, because inheritance is transitive, you may construct an inheritance hierarchy for a set of types. In other words, type D can descend from type C, which derives from type B, which drives from type A, which derives from the base class type A. Members of type A are available to type D because inheritance is transitive.

  • Derived classes do not inherit all members of a base class. The members listed below are not inherited:
  • Static constructors are used to initializing a class's static data.
  • Instance constructors are methods you may use to create a new class instance. Each class is responsible for defining its constructors.
  • Finalizers are methods invoked by the runtime garbage collector to eliminate instances of a class.

In C#, a derived class inherits the members of another class, while a base class inherits the members of another class. The base class’s derived class is a specialized version of the base class. Here is its structure:

class derived-class: base-class 
{ 
   // methods and fields 
   .
   .
}

Visibility of a class

While derived classes inherit all other base class members, their visibility is determined by their accessibility. The accessibility of a member has the following effect on its visibility in derived classes:

  • Private members are only accessible in derived classes nested within their source class, and they are not accessible in derived classes otherwise.
  • Protected members are only visible in derived classes that share the same assembly as the base class, and they are not visible in derived classes housed in a separate group than the base class.
  • Public members are visible in derived classes and form part of the public interface of the derived type. Public inherited members can be accessed the same way as members declared in the derived class can. IN THE FOLLOWING EXAMPLE, Class A creates a method named Method1, and class B inherits from class A. The example then invokes Method1 as if it were a method on B.

Importance of Inheritance

Super Class: A superclass is a class whose characteristics are inherited (or a base class or a parent class).

Subclass: A class that inherits from another class is a subclass (or a derived class, extended class, or child class) in addition to the superclass’s fields and methods.

Reusability: Inheritance supports the idea of “reusability,” which means that if we want to construct a new class and there is already a class that has part of the code that we require, we may derive our new class from the old class. We are utilizing the old class’s fields and functions by doing so.

Other useful articles:


Back to top

© , C Sharp Online — All Rights Reserved - Terms of Use - Privacy Policy