The Apartment , Commercial , and Home Derived ClassesIn this chapter you have created three derived classes from the base Building class. The code for these derived classes is presented in Listing 17.2. Note that each class is in the same file as the Building class. Being able to have multiple classes declared in a single file is something that previous versions of Visual Basic did not allow. This makes it easier to keep the base class and its derived classes collected together in one place. Listing 17.2 The Derived Classes from the Building Class: Apartment , Commercial , and Home' ++++++++++++++++++++++++++++ Apartment Class ++++++++++++++++++++++++ Public Class Apartment Inherits Building Private mUnits As Integer ' The number of apartments Private mRent As Double ' Rent per unit Private mOccupRate As Double ' Occupancy rate for building ' ==================== Properties ====================== Public Property Units() ' Units Get Return mUnits End Get Set(ByVal Value) mUnits = Value End Set End Property Public Property Rents() ' Rents Get Return mRent End Get Set(ByVal Value) mRent = Value End Set End Property Public Property OccupancyRate() ' Occupancy rate Get Return mOccupRate End Get Set(ByVal Value) mOccupRate = Value End Set End Property ' ============== Methods ================== Public Sub DisplayBuilding() DisplayBaseInfo() Console.WriteLine("Number of Units: " & mUnits) Console.WriteLine("Rent per Unit: " & FormatMoney(mRent)) Console.WriteLine("Occupancy Rate: " & mOccupRate) End Sub End Class ' ++++++++++++++++++++++++++++ Commercial Class ++++++++++++++++++++++++++++ Public Class Commercial Inherits Building Private mSquareFeet As Integer ' Rentable square feet Private mRentPerSF As Double ' Rent per square foot Private mParking As Integer ' Parking spots ' ==================== Properties ====================== Public Property SquareFeet() ' Square feet Get Return mSquareFeet End Get Set(ByVal Value) mSquareFeet = Value End Set End Property Public Property RentPerSF() ' Rent per square foot Get Return mRentPerSF End Get Set(ByVal Value) mRentPerSF = Value End Set End Property Public Property ParkingSpots() ' Parking spots Get Return mParking End Get Set(ByVal Value) mParking = Value End Set End Property ' ============== Methods ================== Public Sub DisplayBuilding() DisplayBaseInfo() ' Call base class Console.WriteLine("Square Feet: " & mSquareFeet) Console.WriteLine("Rent per SF: " & FormatMoney(mRentPerSF)) Console.WriteLine("Parking Spots: " & mParking) End Sub End Class ' ++++++++++++++++++++++++++++ Home Class ++++++++++++++++++++++++++++ Public Class Home Inherits Building Private mSquareFeet As Integer ' Home's square feet Private mRentPerMonth As Double ' Rent per month Private mBedrooms As Integer ' Number of bedrooms Private mBaths As Integer ' Number of bathrooms ' ==================== Properties ====================== Public Property SquareFeet() ' Square feet Get Return mSquareFeet End Get Set(ByVal Value) mSquareFeet = Value End Set End Property Public Property Rent() ' Rent Get Return mRentPerMonth End Get Set(ByVal Value) mRentPerMonth = Value End Set End Property Public Property Bedrooms() ' Bedrooms Get Return mBedrooms End Get Set(ByVal Value) mBedrooms = Value End Set End Property Public Property Baths() ' Baths Get Return mBaths End Get Set(ByVal Value) mBaths = Value End Set End Property ' ============== Methods ================== Public Sub DisplayBuilding() DisplayBaseInfo() ' Call base class ' Console.WriteLine("Building Type: Home") Console.WriteLine("Square Feet: " & mSquareFeet) Console.WriteLine("Rent per Month: " & FormatMoney(mRentPerMonth)) Console.WriteLine("Bedrooms: " & mBedrooms) Console.WriteLine("Baths: " & mBaths) End Sub End Class The derived classes are very similar to one another, and the mechanics are the same for them all, so this section explains the code for just one of them. Each derived class begins with the class name and the Inherits keyword: Public Class Apartment Inherits Building The Inherits keyword tells Visual Basic .NET that the Apartment class will inherit all the allowable data members and methods of the base class. (Keep in mind that you can use different flavors of access specifiers to limit access of the derived classes to the member data and methods in the base class.) Member Data and Property AccessorsThe next several lines of each derived class list the member data for the class. The member data properties distinguish the derived class from the base class. For the Apartment class, these members are the number of apartment units ( mUnits ), the rent per month ( mRent ), and the occupancy rate for the apartment complex ( mOccupRate ). An apartment complex "is a" building, and these additional pieces of information supply details that allow you to identify this building as an apartment building. After each of these member data items is declared, the property accessor methods are provided for each item. In this simple example, the Get method returns the value of each member, and the Set method assigns a value to the member. It's interesting to note that Visual Basic .NET is smart enough to make the argument to the Set method match the data type of the data member. Methods of the Derived ClassesThe final section of each class is a method named DisplayBuilding() that is used to display the member data of the class. Note that this method name is the same for all three derived classes. How is this possible? Isn't there going to be a namespace collision for the method if they appear in all three classes? No, there is no namespace collision because the DisplayBuilding() method is encapsulated into each object. Therefore, if you define three objects, such as MyApt , MyOffice , and MyHome , the method can be accessed only with MyApt.DisplayBuilding() , MyOffice.DisplayBuilding() , and MyHome.DisplayBuilding() . In every case, you must prefix the Display method with the object name and dot operator, which prevents any namespace collision in Visual Basic .NET. |