Polymorphism in C# - ByteScout

Polymorphism in C#

‘Poly’ means many, and ‘morphism’ means forms. Hence, Polymorphism represents “many forms,” which arises when several classes are connected by inheritance.

Polymorphism uses the same inheritance approaches to carry out various objectives and enables a single function in several ways.

  1. Benefits of Encapsulation
  2. Polymorphism is Classified into Two Types
  3. Overview of Polymorphism

Polymorphism C#

Benefits of Encapsulation

Data Hiding: The user will be unaware of the class’s internal implementation, and the user will not be able to see how the class keeps values in variables. He knows that we are giving accessor values and that variables are initialized to that value.

Increased Flexibility: Depending on our needs, we may make the class’s variables read-only or write-only.

Reusability: Encapsulation promotes reusability and makes it simple to adapt to new requirements.

Polymorphism is Classified into Two Types

  1. Static Polymorphism- It is also known as compile-time or early binding Polymorphism, and method overloading and operator overloading are used to produce static Polymorphism
  2. Dynamic Polymorphism- It is known as run-time or late binding Polymorphism. Only method overriding is used to accomplish dynamic Polymorphism.

Overview of Polymorphism

Virtual Members

When a derived class inherits from a base class, it inherits all of the base class’s methods, fields, attributes, and events. For the behavior of virtual techniques, the creator of the derived class has many options:

  • The derived class can override virtual members of the base class, hence defining new behavior.
  • The derived class may inherit the closest base class method without overriding it, retaining the present behavior while allowing subsequent derived classes to alter the method.
  • The derived class may declare new non-virtual implementations of the base class’s members that mask the base class’s implementations.
  • Only if the base class member is specified as virtual or abstract may a derived class override it. The derived member must utilize the override keyword to indicate that the method will participate in virtual invocation.

Various Functionalities of Virtual Members

➔    Prevent derived classes from overriding virtual members

Regardless of how many classes have been established between the virtual member and the class that first declared it, virtual members remain virtual.

➔    Access Virtual members of base classes from derived classes

A derived class that has replaced or overridden a method or property on the base class can still use the base keyword to access the method or property on the base class.

➔    Hide members of the base class with new members

If you want a member in your derived class to have the same name as a member in a base class, use the new keyword to conceal the base class member.


About the Author

ByteScout Team ByteScout Team of Writers ByteScout has a team of professional writers proficient in different technical topics. We select the best writers to cover interesting and trending topics for our readers. We love developers and we hope our articles help you learn about programming and programmers.