Object Inheritance (Polymorphic Code) C#

One of the fundamental features of object-oriented programming is inheritance. It is a concept used to define a child class that reuses, modifies, or extends the properties of a parent class. In Inheritance, you must know the parent class and the subclass. The parent class (also known as the base class or superclass) is the class whose behaviors are inherited. On the other hand, the subclass (also known as the derived class or child class) is the class that reuses the behaviors of the base class.

C# only supports single inheritance. This type of inheritance means that a derived class can only inherit the properties of a single base class. However, you can still define an inheritance hierarchy for a set of types because inheritance is transitive. In simple terms, what we mean is type 4 can inherit from type 3, which inherits from type 2, which inherits from the base class type 1. The members of type 1 will be available in type 4 because inheritance in C# is transitive.

Derived classes do not inherit all members of a base class. In C sharp, the following members are not inherited:

  • Static constructors – initializes the static data of a class
  • Finalizers – They are called by the runtime garbage collectors. Finalizers destroy instances of a class
  • Instance constructors – They are called to create a new instance of the class. Each class defines its own instance constructors.

All other members of a base class are inherited by the derived class. However, their accessibility determines whether they are visible or not. Our C# programming assignment help experts have explained how member’s accessibility affects its visibility for derived classes below:

  • Private members – only the members that are nested in the base class are visible in derived classes.
  • Protected members –  Are visible only in derived classes
  • Internal members – These members are visible in derived classes located in the same assembly as the base class. Internal members are not visible in subclasses that are located in a different assembly from the base class
  • Public members – These members are part of the derived class public interface. They are visible in the derived class. Also, we can call public members as if they were defined in the derived class.

Derived classes can also provide an alternate implementation by overriding inherited members. A member can only be overridden if it is marked with the virtual keyword. Parent class members are not marked as virtual by default. For this reason, they cannot be overridden. Any attempt to override a non-virtual member will generate a compiler error CS0506.

There are also cases where a derived class must override the parent class implementation. Derived classes must override base class members with the abstract keyword. A CS0534 error is generated if the derived class does not provide the implementation of the base class.

Categories such as structs, enums, and delegates do not support inheritance. This concept only applies to classes and interfaces. A compiler error CS0527 will be generated. This error denotes that although we can define interfaces implemented by a struct, inheritance is not supported.

Implicit Inheritance

In C# and .NET all types in the system inherit from ‘Object’ or a type derived from it. In other words, any type has access to the common functionality of ‘Object’. Let us define a new class – SimpleClass- to see what implicit inheritance means. SimpleClass is an empty class definition.

Public class SimpleClass

{ }

A reflection can help us inspect the metadata of a type in order to get information about that type. We can use a reflection to get a list of the members that belong to the SimpleClass type. The output of our above example indicates that it actually has nine members. However, we have not defined any members of SimpleClass. One of the members is a default constructor (has no parameters). The C# compiler automatically supplies it to the class. The other eight are members of ‘Object.’ These are the types that all interfaces and classes in the .NET type system implicitly inherit.

The methods mentioned below are available to our SimpleClass because of implicit inheritance

  • Public ToString method

It converts a class object to its string representation. It returns the fully qualified type name. In our case, the public ToString method will return the string “SimpleClass.”

  • Three methods

These methods test the equality of two objects. The three methods test the reference equality by default, i.e. two object variables must refer to the same object to be equal.

  • The publicGetHashCode method

It computes the value that allows an instance of the type to be used in hashed collections.

  • The public GetType method

This method returns a Type object that represents the Simpleclass type.

  • The protected Finalize method

It is designed to free unmanaged resources before an object’s memory is reclaimed by the garbage collector.

  • The protected MemberwiseClone method

This method creates a shallow clone of the current object.

Do you want a detailed copy of object inheritance and polymorphic codes in C#? Avail of our help with C sharp assignment now.