Download presentation
Presentation is loading. Please wait.
Published byMorgan Derek Stafford Modified over 8 years ago
1
Visual Basic 2010 How to Program © 1992-2011 by Pearson Education, Inc. All Rights Reserved.
3
This chapter continues our discussion of object-oriented programming by introducing inheritance. It provides a method of software reuse in which a new class is created quickly and easily by absorbing an existing class’s members and customizing them with new or modified capabilities. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.
4
When creating a class, rather than declaring completely new members, you can designate that the new class inherits the members of an existing class. The existing class is called the base class, and the new class is the derived class. A derived class can add its own instance variables, Shared variables, properties and methods, and it can customize methods and properties it inherits. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.
5
We explain and demonstrate polymorphism, which enables you to conveniently program “in the general” rather than “in the specific.” You’ll see that polymorphism simplifies programming with classes and makes it easy to extend systems with new capabilities. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.
6
Inheritance enables an is-a relationship. In an is-a relationship, an object of a derived class also can be treated as an object of its base class. For example, a car is a vehicle. Figure 10.1 lists several simple examples of base classes and derived classes—base classes tend to be more general and derived classes tend to be more specific. Base-class objects cannot be treated as objects of their derived classes—although all cars are vehicles, not all vehicles are cars (the other vehicles could be trucks, planes or bicycles, for example) © 1992-2011 by Pearson Education, Inc. All Rights Reserved.
7
Figure below lists several simple examples of base classes and derived classes—base classes tend to be more general and derived classes tend to be more specific.
8
© 1992-2011 by Pearson Education, Inc. All Rights Reserved. Figure below shows a sample UML class diagram of an inheritance hierarchy.
9
We use a business-oriented inheritance hierarchy containing types of employees in a company’s payroll application. All employees of the company have a lot in common ◦ Base Class: Commission employees (who will be represented as objects of a base class) are paid a percentage of their sales, ◦ Derived Class: Base-salaried commission employees (who will be represented as objects of a derived class) receive a percentage of their sales plus a base salary. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.
10
First, we present base class CommissionEmployee. Next, we create a derived class BasePlusCommissionEmployee that inherits from class CommissionEmployee. Then we present an application that creates a BasePlusCommissionEmployee object and demonstrates that it has all the capabilities of the base class and the derived class, but calculates its earnings differently. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.
11
Consider class CommissionEmployee (Fig. 10.4). The Public services of class CommissionEmployee include: ◦ a constructor to initialize values ◦ properties FirstName and LastName SocialSecurityNumber, GrossSales, and CommissionRate ◦ methods CalculateEarnings and ToString The class also declares Private instance variables grossSalesValue and commissionRate-Value © 1992-2011 by Pearson Education, Inc. All Rights Reserved.
12
Defining Base Class
13
© 1992-2011 by Pearson Education, Inc. All Rights Reserved.
16
Method CalculateEarnings and Declaring Methods Overridable ◦ Method CalculateEarnings (lines 55–57) calculates a CommissionEmployee ’s earn-ings. ◦ A base-class method must be declared Overridable if a derived class should be allowed to override the method with a version more appropriate for that class. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.
17
When we create class BasePlusCommissionEmployee, we’ll want to override (redefine) CommissionEmployee ’s Calculate-Earnings method to customize the earnings calculation for a BasePlusCommissionEmployee. For this reason, we declared CalculateEarnings as Overridable in line 55. In BasePlusCommissionEmployee, we’ll declare method CalculateEarnings with the keyword Overrides. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.
18
Method ToString and Overriding Base Class Methods: ◦ Method ToString (lines 60–66) returns a String containing information about the CommissionEmployee. ◦ The keyword Overrides (line 60) indicates that this method overrides (redefines) the version of ToString that was inherited from CommissionEmployee ’s base class (that is, Object ). © 1992-2011 by Pearson Education, Inc. All Rights Reserved.
19
Most of a BasePlusCommissionEmployee ’s capabilities are similar, if not identical, to the those of class CommissionEmployee Both classes require instance variables for the first name, last name, social security number, gross sales and commission rate, and properties and methods to manipulate that data. To create class BasePlusCommissionEmployee without using inheritance, we probably would have copied the code from class CommissionEmployee and pasted it into class BasePlusCommissionEmployee, then modified the new class to include a base salary instance variable, and the methods and properties that manipulate the base salary, including a new CalculateEarnings method. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.
20
Declaring Class BasePlusCommissionEmployee (Fig. 10.6) ◦ A BasePlusCommissionEmployee is a CommissionEmployee (because inheritance passes on the capabilities of class CommissionEmployee ), but class it also has An instance variable baseSalaryValue A property BaseSalary. Also, BasePlusCommissionEmployee provides ◦ a new constructor ◦ a customized version of method CalculateEarnings ◦ a customized version of method ToString © 1992-2011 by Pearson Education, Inc. All Rights Reserved.
21
Inherits properties and methods of CommissionEmployee
22
© 1992-2011 by Pearson Education, Inc. All Rights Reserved.
24
Inheriting from Class CommissionEmployee Keyword Inherits of the class declaration indicates that class BasePlusCommissionEmployee inherits all of the Public members as well as Protected members of class CommissionEmployee. We do not redeclare the base class’s Private instance variables—these are nevertheless present (but hidden) in derived class objects. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.
25
BasePlusCommissionEmployee Constructor ◦ Each derived-class constructor must implicitly or explicitly call its base-class constructor to ensure that the instance variables inherited from the base class are properly initialized. ◦ BasePlusCommissionEmployee ’s six-argument constructor explicitly calls class CommissionEmployee ’s five-argument constructor to initialize the base class portion of a BasePlusCommissionEmployee object (that is, the five instance variables from class CommissionEmployee ). © 1992-2011 by Pearson Education, Inc. All Rights Reserved.
26
Overriding Method CalculateEarnings ◦ Class BasePlusCommissionEmployee ’s CalculateEarnings method overrides class CommissionEmployee ’s CalculateEarnings method to calculate the earnings of a base-salaried commission employee. ◦ The new version obtains the portion of the employee’s earnings based on commission alone by calling CommissionEmployee ’s CalculateEarnings method with the expression MyBase.CalculateEarnings(). © 1992-2011 by Pearson Education, Inc. All Rights Reserved.
27
Figure 10.6 below tests derved class BasePlusCommissionEmployee. Lines 9–10 create a BasePlusCommissionEmployee object and pass "Bob", "Lewis", "333-33-3333", 5000, 0.04 and 300 to the constructor as the first name, last name, social security number, gross sales, commission rate and base salary, respectively. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.
28
Using properties of derived object: employee
29
© 1992-2011 by Pearson Education, Inc. All Rights Reserved. Using Calculatearning and ToString methods of derived object: employee
30
Lines 13–22 use BasePlusCommissionEmployee ’s properties to output the object’s data. Notice that we’re able to access all of the Public properties of classes CommissionEmployee and BasePlusCommissionEmployee here. Lines 25–26 calculate and display the BasePlusCommissionEmployee ’s earnings by calling its Caculate-Earnings method. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.
32
Homework 10.9
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.