<< Chapter < Page Chapter >> Page >

The effect of extending a class

The effect of having the Balloon class extend the Airship class is different from anything that you have seen in previous modules. When one class extends another class, the new class inherits all ofthe properties, events, and methods of the superclass and all of its superclasses.

Airship extends Object

In this case, the Airship class extends the Object class by default. Therefore, an object instantiated from the Balloon class contains:

  • The two properties defined in the Balloon class in Listing 2 .
  • The two properties defined in the Airship class in Listing 1 .
  • The eight methods defined in the Object class discussed earlier .

The beginning of the Driver class

At this point, I am going to show and explain the first half of the Driver class (see Listing 3 ) and relate it to the program output shown in Figure 1 .

Listing 3 . The beginning of the Driver class.

using System;namespace Airship01 { //Define a class to exercise the Balloon class and the// Airplane class. class Driver {static void Main(string[] args) {Balloon balloon = new Balloon(); balloon.range = 5;balloon.altitude = 500; balloon.passengerCapacity = 5;balloon.liftMedia = "Hot Air"; Console.WriteLine("Balloon");Console.WriteLine( "range = " + balloon.range + " miles");Console.WriteLine( "altitude = " + balloon.altitude + " feet");Console.WriteLine("passenger capacity = " + balloon.passengerCapacity);Console.WriteLine("lift media = " + balloon.liftMedia);

For convenience and because of their small sizes, I elected to define all four classes in the same file as the file that contains the Driver class with the Main method. That is not a requirement, however, and on large projects you may want to put each class definition inits own file.

Code common to all four classes

The single line of code at the very beginning of Listing 3 applies to all four classes. You have seen this before so it should not be new to you.

Instantiate a new Balloon object

The Main method begins by instantiating a new object of the Balloon class and saving its reference in a reference variable of type Balloon named balloon . This reference will be used later to access the new object.

Set properties in the Balloon object

Then the Main method calls the four set methods belonging to the new Balloon object, using them to set values into the four properties belonging to the Balloon object.

Remember that the set methods that hide two of the properties are defined in the Airship class and are inherited into the Balloon class. The other two set methods are defined in the Balloon class.

Get and display property values

After that, the Main method calls the four get methods belonging to the new Balloon object, using them to get and display values from the four properties belonging to the new Balloon object.

The get methods that hide two of the properties are defined in the Airship class and are inherited into the Balloon class. The other two get methods are defined in the Balloon class.

Questions & Answers

what is chemistry
ISIYAKA Reply
what is oxidation
Chidiebube Reply
calculate molarity of NaOH solution when 25.0ml of NaOH titrated with 27.2ml of 0.2m H2SO4
Gasin Reply
what's Thermochemistry
rhoda Reply
the study of the heat energy which is associated with chemical reactions
Kaddija
How was CH4 and o2 was able to produce (Co2)and (H2o
Edafe Reply
explain please
Victory
First twenty elements with their valences
Martine Reply
what is chemistry
asue Reply
what is atom
asue
what is the best way to define periodic table for jamb
Damilola Reply
what is the change of matter from one state to another
Elijah Reply
what is isolation of organic compounds
IKyernum Reply
what is atomic radius
ThankGod Reply
Read Chapter 6, section 5
Dr
Read Chapter 6, section 5
Kareem
Atomic radius is the radius of the atom and is also called the orbital radius
Kareem
atomic radius is the distance between the nucleus of an atom and its valence shell
Amos
Read Chapter 6, section 5
paulino
Bohr's model of the theory atom
Ayom Reply
is there a question?
Dr
when a gas is compressed why it becomes hot?
ATOMIC
It has no oxygen then
Goldyei
read the chapter on thermochemistry...the sections on "PV" work and the First Law of Thermodynamics should help..
Dr
Which element react with water
Mukthar Reply
Mgo
Ibeh
an increase in the pressure of a gas results in the decrease of its
Valentina Reply
definition of the periodic table
Cosmos 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