<< Chapter < Page Chapter >> Page >

However, while necessary, that is not sufficient to ensure runtime polymorphic behavior.

Must define or inherit the method

The type of the reference variable must also be the name of a class that either defines or inherits a version of the method that will ultimately becalled on the object.

Since the class named Object does not define (or inherit) the method named m , a reference of type Object does not qualify as a participant in runtime polymorphic behavior in this case. The attempt to use it as a participant results in the compiler error given above.

One additional scenario

Before leaving this topic, let's look at one additional scenario to help you distinguish what is, and what is not, runtime polymorphism. Consider the codeshown in Listing 6 .

Listing 6 . Not polymorphic behavior.

//Instantiate obj of class A var = new A();//Call the method on it ((A)var).m();// Pause until the user presses any key. Console.ReadKey();}//end Main }//end class Polymorph03

A new object of type A

The code in Listing 6 instantiates a new object of the class named A , and stores the object's reference in the original reference variable named var of type Object .

As a side note, this overwrites the previous contents of the reference variable with a new reference and causes the object whose reference waspreviously stored there to become eligible for garbage collection.

Downcast and call the method

Then the code in Listing 6 casts the reference down to type A , (the type of the object to which the reference refers) , and calls the method named m on the downcast reference.

The output

As you would probably predict, this produces the following output on the computer screen:

m in class A

In this case, the version of the method defined in the class named A , (not the version defined in B) was executed.

Not polymorphic behavior

Once again, in my view, this is not runtime polymorphic behavior (at least it isn't a very useful form of polymorphic behavior) . This code simply converts the type of the reference from type Object to the type of the class from which the object was instantiated, and calls one ofits methods. Nothing special takes place regarding a selection among different versions of the method.

Once again, what is runtime polymorphism?

As I have discussed in this module, runtime polymorphic behavior based on inheritance occurs when

  • The type of the reference is a superclass of the class from which the object was instantiated.
  • The version of the method that is executed is the version that is either defined in, or inherited into, the class from which the object wasinstantiated.

And that is probably more than you ever wanted to hear about runtime polymorphism based on inheritance.

A future module will discuss runtime polymorphism based on the C# interface. From a practical viewpoint, you will find the rules to be similar but somewhatdifferent in the case of the C# interface.

Run the program

I encourage you to copy the code from Listing 7 . Use that code to create a C# console project. Compile and run the project. Experiment with the code,making changes, and observing the results of your changes. Make certain that you can explain why your changes behave as they do.

Questions & Answers

what is mutation
Janga Reply
what is a cell
Sifune Reply
how is urine form
Sifune
what is antagonism?
mahase Reply
classification of plants, gymnosperm features.
Linsy Reply
what is the features of gymnosperm
Linsy
how many types of solid did we have
Samuel Reply
what is an ionic bond
Samuel
What is Atoms
Daprince Reply
what is fallopian tube
Merolyn
what is bladder
Merolyn
what's bulbourethral gland
Eduek Reply
urine is formed in the nephron of the renal medulla in the kidney. It starts from filtration, then selective reabsorption and finally secretion
onuoha Reply
State the evolution relation and relevance between endoplasmic reticulum and cytoskeleton as it relates to cell.
Jeremiah
what is heart
Konadu Reply
how is urine formed in human
Konadu
how is urine formed in human
Rahma
what is the diference between a cavity and a canal
Pelagie Reply
what is the causative agent of malaria
Diamond
malaria is caused by an insect called mosquito.
Naomi
Malaria is cause by female anopheles mosquito
Isaac
Malaria is caused by plasmodium Female anopheles mosquitoe is d carrier
Olalekan
a canal is more needed in a root but a cavity is a bad effect
Commander
what are pathogens
Don Reply
In biology, a pathogen (Greek: πάθος pathos "suffering", "passion" and -γενής -genēs "producer of") in the oldest and broadest sense, is anything that can produce disease. A pathogen may also be referred to as an infectious agent, or simply a germ. The term pathogen came into use in the 1880s.[1][2
Zainab
A virus
Commander
Definition of respiration
Muhsin Reply
respiration is the process in which we breath in oxygen and breath out carbon dioxide
Achor
how are lungs work
Commander
where does digestion begins
Achiri Reply
in the mouth
EZEKIEL
what are the functions of follicle stimulating harmones?
Rashima Reply
stimulates the follicle to release the mature ovum into the oviduct
Davonte
what are the functions of Endocrine and pituitary gland
Chinaza
endocrine secrete hormone and regulate body process
Achor
while pituitary gland is an example of endocrine system and it's found in the Brain
Achor
what's biology?
Egbodo Reply
Biology is the study of living organisms, divided into many specialized field that cover their morphology, physiology,anatomy, behaviour,origin and distribution.
Lisah
biology is the study of life.
Alfreda
Biology is the study of how living organisms live and survive in a specific environment
Sifune
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