<< 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 the stm
Brian Reply
is there industrial application of fullrenes. What is the method to prepare fullrene on large scale.?
industrial application...? mmm I think on the medical side as drug carrier, but you should go deeper on your research, I may be wrong
How we are making nano material?
what is a peer
What is meant by 'nano scale'?
What is STMs full form?
scanning tunneling microscope
how nano science is used for hydrophobicity
Do u think that Graphene and Fullrene fiber can be used to make Air Plane body structure the lightest and strongest. Rafiq
what is differents between GO and RGO?
what is simplest way to understand the applications of nano robots used to detect the cancer affected cell of human body.? How this robot is carried to required site of body cell.? what will be the carrier material and how can be detected that correct delivery of drug is done Rafiq
what is Nano technology ?
Bob Reply
write examples of Nano molecule?
The nanotechnology is as new science, to scale nanometric
nanotechnology is the study, desing, synthesis, manipulation and application of materials and functional systems through control of matter at nanoscale
Is there any normative that regulates the use of silver nanoparticles?
Damian Reply
what king of growth are you checking .?
What fields keep nano created devices from performing or assimulating ? Magnetic fields ? Are do they assimilate ?
Stoney Reply
why we need to study biomolecules, molecular biology in nanotechnology?
Adin Reply
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
what school?
biomolecules are e building blocks of every organics and inorganic materials.
anyone know any internet site where one can find nanotechnology papers?
Damian Reply
sciencedirect big data base
Introduction about quantum dots in nanotechnology
Praveena Reply
what does nano mean?
Anassong Reply
nano basically means 10^(-9). nanometer is a unit to measure length.
do you think it's worthwhile in the long term to study the effects and possibilities of nanotechnology on viral treatment?
Damian Reply
absolutely yes
how to know photocatalytic properties of tio2 nanoparticles...what to do now
Akash Reply
it is a goid question and i want to know the answer as well
characteristics of micro business
for teaching engĺish at school how nano technology help us
How can I make nanorobot?
Do somebody tell me a best nano engineering book for beginners?
s. Reply
there is no specific books for beginners but there is book called principle of nanotechnology
how can I make nanorobot?
what is fullerene does it is used to make bukky balls
Devang Reply
are you nano engineer ?
fullerene is a bucky ball aka Carbon 60 molecule. It was name by the architect Fuller. He design the geodesic dome. it resembles a soccer ball.
what is the actual application of fullerenes nowadays?
That is a great question Damian. best way to answer that question is to Google it. there are hundreds of applications for buck minister fullerenes, from medical to aerospace. you can also find plenty of research papers that will give you great detail on the potential applications of fullerenes.
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!

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?