<< Chapter < Page Chapter >> Page >

An inherited virtual method can be overridden by every class that inherits it, resulting in potentially many differentoverridden versions of a given method in a class hierarchy.

Listing 3 . Definition of class C.

class C : B { public override String ToString() {return "ToString in class C"; }//end overridden ToString()}//end class B

Behavior of overridden version

The behavior of this overridden version of the method is similar to, but different from the overridden version in the class B.

In this case, the method returns a reference to a String object that can be used to confirm that this overridden version of the methodhas been executed.

The driver class

Listing 4 shows the beginning of the driver class named Polymorph04 .

Listing 4 . Beginning of the driver class.

public class Polymorph04 { public static void Main() {Object varA = new A(); String v1 = varA.ToString();Console.WriteLine(v1);

A new object of the class A

The Main method of the driver class begins by instantiating a new object of the class A, and saving the object's reference in a reference variable of type Object , named varA .

It is very important to note that even though the object was instantiated from the class named A , the reference was saved as type Object , not type A.

Call ToString method on the reference

Then the code in Listing 4 calls the ToString method on the reference variable named varA , saving the returned reference to the String object in a reference variable of type String named v1 .

Display the returned String

Finally, that reference is passed to the WriteLine method, causing the String returned by the ToString method to be displayed on the computer screen. This causes the text shown in Figure 1 to be displayed.

Figure 1 . Text output for ToString method and class A.


Default ToString behavior

What you are seeing here is the String produced by the default version of the ToString method, as defined by the class named Object . The default implementation of the ToString method returns the fully qualified name of the type of the object. In this case, the object was instantiated from the class named A (so the type of the object is A ) .

Class A does not override ToString

Recall that our new class named A does not override the ToString method. Therefore, when the ToString method is called on a reference to an object of the class A, the default version of the method is executed, producing the output shown in Figure 1 .

A new object of the class B

Now consider the code shown in Listing 5 , which instantiates a new object of the class named B , and stores the object's reference in a reference variable of type Object .

Listing 5 . A new object of the class B.

Object varB = new B(); String v2 = varB.ToString();Console.WriteLine(v2);

Call ToString and display the result

The code in Listing 5 calls the ToString method on the reference of type Object , saving the returned reference in the reference variable named v2 . (Recall that the ToString method is overridden in the class named B .)

As before, the reference is passed to the WriteLine method, which causes the text shown in Figure 2 to be displayed on the computer screen.

Questions & Answers

Do somebody tell me a best nano engineering book for beginners?
s. Reply
what is fullerene does it is used to make bukky balls
Devang Reply
are you nano engineer ?
what is the Synthesis, properties,and applications of carbon nano chemistry
Abhijith Reply
so some one know about replacing silicon atom with phosphorous in semiconductors device?
s. Reply
Yeah, it is a pain to say the least. You basically have to heat the substarte up to around 1000 degrees celcius then pass phosphene gas over top of it, which is explosive and toxic by the way, under very low pressure.
how to fabricate graphene ink ?
for screen printed electrodes ?
What is lattice structure?
s. Reply
of graphene you mean?
or in general
in general
Graphene has a hexagonal structure
On having this app for quite a bit time, Haven't realised there's a chat room in it.
what is biological synthesis of nanoparticles
Sanket Reply
what's the easiest and fastest way to the synthesize AgNP?
Damian Reply
types of nano material
abeetha Reply
I start with an easy one. carbon nanotubes woven into a long filament like a string
many many of nanotubes
what is the k.e before it land
what is the function of carbon nanotubes?
I'm interested in nanotube
what is nanomaterials​ and their applications of sensors.
Ramkumar Reply
what is nano technology
Sravani Reply
what is system testing?
preparation of nanomaterial
Victor Reply
Yes, Nanotechnology has a very fast field of applications and their is always something new to do with it...
Himanshu Reply
good afternoon madam
what is system testing
what is the application of nanotechnology?
In this morden time nanotechnology used in many field . 1-Electronics-manufacturad IC ,RAM,MRAM,solar panel etc 2-Helth and Medical-Nanomedicine,Drug Dilivery for cancer treatment etc 3- Atomobile -MEMS, Coating on car etc. and may other field for details you can check at Google
anybody can imagine what will be happen after 100 years from now in nano tech world
after 100 year this will be not nanotechnology maybe this technology name will be change . maybe aftet 100 year . we work on electron lable practically about its properties and behaviour by the different instruments
name doesn't matter , whatever it will be change... I'm taking about effect on circumstances of the microscopic world
how hard could it be to apply nanotechnology against viral infections such HIV or Ebola?
silver nanoparticles could handle the job?
not now but maybe in future only AgNP maybe any other nanomaterials
I'm interested in Nanotube
this technology will not going on for the long time , so I'm thinking about femtotechnology 10^-15
can nanotechnology change the direction of the face of the world
Prasenjit Reply
At high concentrations (>0.01 M), the relation between absorptivity coefficient and absorbance is no longer linear. This is due to the electrostatic interactions between the quantum dots in close proximity. If the concentration of the solution is high, another effect that is seen is the scattering of light from the large number of quantum dots. This assumption only works at low concentrations of the analyte. Presence of stray light.
Ali Reply
how did you get the value of 2000N.What calculations are needed to arrive at it
Smarajit Reply
Privacy Information Security Software Version 1.1a
Got questions? Join the online conversation and get instant answers!
QuizOver.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?