<< 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

summarize halerambos & holbon
David Reply
the Three stages of Auguste Comte
Clementina Reply
what are agents of socialization
Antonio Reply
sociology of education
Nuhu Reply
definition of sociology of education
Nuhu
what is culture
Abdulrahim Reply
shared beliefs, values, and practices
AI-Robot
What are the two type of scientific method
ogunniran Reply
I'm willing to join you
Aceng Reply
what are the scientific method of sociology
Man
what is socialization
ogunniran Reply
the process wherein people come to understand societal norms and expectations, to accept society's beliefs, and to be aware of societal values
AI-Robot
scientific method in doing research
ogunniran
defimition of sickness in afica
Anita
Cosmology
ogunniran
Hmmm
ogunniran
list and explain the terms that found in society
REMMY Reply
list and explain the terms that found in society
Mukhtar
what are the agents of socialization
Antonio
Family Peer group Institution
Abdulwajud
I mean the definition
Antonio
ways of perceived deviance indifferent society
Naomi Reply
reasons of joining groups
SAM
to bring development to the nation at large
Hyellafiya
entails of consultative and consensus building from others
Gadama
World first Sociologist?
Abu
What is evolutionary model
Muhammad Reply
Evolution models refer to mathematical and computational representations of the processes involved in biological evolution. These models aim to simulate and understand how species change over time through mechanisms such as natural selection, genetic drift, and mutation. Evolutionary models can be u
faruk
what are the modern trends in religious behaviours
Selekeye Reply
what are social norms
Daniel Reply
shared standards of acceptable behavior by the group or appropriate behavior in a particular institution or those behaviors that are acceptable in a society
Lucius
that is how i understood it
Lucius
examples of societal norms
Diamond
Discuss the characteristics of the research located within positivist and the interpretivist paradigm
Tariro Reply
what is Industrialisation
Selekeye Reply
industrialization
Angelo
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