<< Chapter < Page Chapter >> Page >

Class B extends class A, which extends Object

Upon examination of the program, you will see that the class named A extends the class named Object . You will also see that the class named B extends the class named A .

The class named Polymorph01 is a driver class whose Main method exercises the methods defined in the classes named A and B .

Designed to illustrate method overloading

Once again, this program is not intended to correspond to any particular real-world scenario. Rather, it is a very simple program designed specificallyto illustrate method overloading.

Discussion and sample code

Will discuss in fragments

As usual, I will discuss this program in fragments. A complete listing is provided in Listing 4 near the end of the module.

The class named A

The code in Listing 1 defines the class named A , which explicitly extends Object .

Listing 1 . Class A from the project named Polymorph01.

using System; class A : Object {public void m() { Console.WriteLine("m()");}//end method m() }//end class A

Redundant code

Recall that explicitly extending Object is not required (but it also doesn't hurt anything).

The class named A would extend the class named Object by default unless the class named A explicitly extends some other class.

The method named m()

The code in Listing 1 defines a method named m() . Note that this version of the method has an empty argument list (it doesn't receive anyparameters when it is executed). The behavior of the method is simply to display a message indicating that it is executing.

The class named B

Listing 2 contains the definition for the class named B . This class extends the class named A and inherits the method named m defined in the class named A .

Listing 2 . Class B from the project named Polymorph01.

class B : A { public void m(int x) {Console.WriteLine("m(int x)"); }//end method m(int x)//---------------------------------// public void m(String y) {Console.WriteLine("m(String y)"); }//end method m(String y)}//end class B

Overloaded methods

In addition to the inherited method named m , the class named B defines two additional overloaded versions of the method named m :

  • m(int x)
  • m(String y)

Note that each of these versions of the method receives a single parameter and the type of the parameter is different in each case.

As with the version of the method having the same name defined in the class named A , the behavior of each of these two methods is simply to display a message indicating that it is executing.

The driver class

Listing 3 contains the definition of the driver class named Polymorph01 .

Listing 3 . Class Polymorph01 from the project named Polymorph01.

public class Polymorph01 { public static void Main() {B var = new B(); var.m();var.m(3); var.m("String");//Pause until the user presses a key. Console.ReadKey();}//end Main }//end class Polymorph01

Call all three overloaded methods

The code in the Main method

  • Instantiates a new object of the class named B
  • Successively calls each of the three overloaded versions of the method named m on the reference to that 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