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

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