<< Chapter < Page Chapter >> Page >
Such a conversion will often result in the loss of data, and that loss is the responsibility of the programmerwho performs the cast.

Assignment compatibility for references

Assignment compatibility for references doesn't involve range issues, as is the case with primitives. Rather, the reference to an object instantiated from agiven class can be assigned to (is assignment-compatible with):

  1. Any reference variable whose type is the same as the class from which the object was instantiated.
  2. Any reference variable whose type is a superclass of the class from which the object was instantiated.
  3. Any reference variable whose type is an interface that is implemented by the class from which the object was instantiated.
  4. Any reference variable whose type is an interface that is implemented by a superclass of the class from which the object was instantiated.
  5. A couple of other cases involving interfaces that extend other interfaces.

In this module, we are interested only in cases 1 and 2 above. We will be interested in the other cases in future modules involving interfaces.

Such an assignment does not require the use of a cast operator.

Type Object is completely generic

As mentioned earlier, a reference to any object can be assigned to a reference variable of the type Object , because the Object class is a superclass of every other class.

Converting reference types with a cast

Assignments of references, other than those listed above, require the use of a cast operator to purposely change the type of the reference.

However, it is not possible to perform a successful cast to convert the type of a reference to another type in all cases.

Generally, a cast can only be performed among reference types that fall on the same ancestral line of the class hierarchy, or on an ancestral line of aninterface hierarchy. For example, a reference cannot be successfully cast to the type of a sibling or a cousin in the class hierarchy.

Downcasting

When we cast a reference along the class hierarchy in a direction away from the root class Object toward the leaves , we often refer to it as a downcast .

While it is also possible to cast in the direction from the leaves to the root , this conversion happens automatically, and the use of a cast operator is not required.

Preview

A sample program is provided that illustrates much of the detail involved in type conversion, method invocation, and casting with respect to referencetypes.

Discussion and sample code

The program named Polymorph02 , shown in Listing 11 near the end of the module illustrates the use of the cast operator with references.

When you examine that program, you will see that two classes named A and C each extend the class named Object . Hence, we might say that they are siblings in the class hierarchy.

Another class named B extends the class named A . Thus, we might say that A is a child of Object , and B is a child of A .

The class named A

The definition of the class named A is shown in Listing 1 . This class implicitly extends the class named Object by default.

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