<< Chapter < Page Chapter >> Page >

How does C# implement polymorphism?

Polymorphism manifests itself in C# in the form of multiple methods having the same name.

In some cases, multiple methods have the same name, but have different formal argument lists. These are overloaded methods.

In other cases, multiple methods have the same name, same return type, and same formal argument list. These are overridden methods.

This module concentrates on the use of method overloading to achieve compile-time polymorphism.

The class named Object

As you learned in an earlier module, every class in C# is a direct or indirect subclass of the class named Object . Methods defined in the Object class are inherited into all other classes. Some of those inherited methods may be overridden to make their behavior more appropriate for objects instantiated from the new subclasses. However, thismodule is not about overriding methods. Instead it is about overloading methods. I will cover method overriding in future modules.

Multiple methods with the same name

Overloaded methods have the same name and different formal argument lists. They may or may not have the same return type.

Polymorphism manifests itself in C# in the form of multiple methods having the same name. As mentioned above, this module concentrates on method overloading , sometimes referred to as compile-time polymorphism . Subsequent modules concentrate on method overriding , sometimes referred to as runtime polymorphism .

Compile-time and runtime polymorphism, what's the difference?

During the compilation and execution of polymorphic code, the compiler and the runtime system must decide which of two or more methods having the same namein the same scope must be executed. With method overloading, that decision is made when the program is compiled. With method overriding, that decision isdeferred and made at runtime. Hence we have the terms compile-time polymorphism and runtime polymorphism .

Every class extends some other class

Every class in C# (except for the class named Object ) extends some other class. If you don't explicitly specify the class that yournew class extends, it will automatically extend the class named Object .

A class hierarchy

Thus, all classes in C# exist in a class hierarchy where the class named Object forms the root of the hierarchy.

Some classes extend Object directly, while other classes are subclasses of Object further down the hierarchy (they extend classes that extend classes that extend Object ).

Methods in the Object class

You learned in an earlier module that the class named Object defines default versions of the following methods and that every class in thehierarchy inherits them:

  • Equals - Overloaded. Determines whether two Object instances are equal.
    • Equals(Object) - Determines whether the specified Object is equal to the current Object.
    • Equals(Object,Object) - Determines whether the specified Object instances are considered equal.
  • Finalize - Allows an Object to attempt to free resources and perform other cleanup operations before the Object isreclaimed by garbage collection.
  • GetHashCode - Serves as a hash function for a particular type.
  • GetType - Gets the Type of the current instance.
  • MemberwiseClone - Creates a shallow copy of the current Object.
  • ReferenceEquals - Determines whether the specified Object instances are the same instance.
  • ToString - Returns a String that represents the current Object.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Xna game studio. OpenStax CNX. Feb 28, 2014 Download for free at https://legacy.cnx.org/content/col11634/1.6
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Xna game studio' conversation and receive update notifications?

Ask