<< 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.

Questions & Answers

what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
cell is the building block of life.
Condoleezza Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

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