Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2009 Pearson Education, Inc. All rights reserved. 1 11 Object-Oriented Programming: Inheritance.

Similar presentations


Presentation on theme: " 2009 Pearson Education, Inc. All rights reserved. 1 11 Object-Oriented Programming: Inheritance."— Presentation transcript:

1  2009 Pearson Education, Inc. All rights reserved. 1 11 Object-Oriented Programming: Inheritance

2  2009 Pearson Education, Inc. All rights reserved. 2 Say not you know another entirely, till you have divided an inheritance with him. – Johann Kasper Lavater This method is to define as the number of a class the class of all classes similar to the given class. – Bertrand Russell Good as it is to inherit a library, it is better to collect one. – Augustine Birrell Save base authority from others’ books. – William Shakespeare

3  2009 Pearson Education, Inc. All rights reserved. 3 OBJECTIVES In this chapter you will learn:  How inheritance promotes software reusability.  The concepts of base classes and derived classes.  To create a derived class that inherits attributes and behaviors from a base class.  To use access modifier protected to give derived-class methods access to base-class members.  To access base-class members with base.  How constructors are used in inheritance hierarchies.  The methods of class object, the direct or indirect base class of all classes.

4  2009 Pearson Education, Inc. All rights reserved. 4 11.1 Introduction 11.2 Base Classes and Derived Classes 11.3 protected Members 11.4 Relationship between Base Classes and Derived Classes 11.5 Constructors in Derived Classes 11.6 Software Engineering with Inheritance 11.7 Class object

5  2009 Pearson Education, Inc. All rights reserved. 5 11.1 Introduction Inheritance allows a new class to absorb an existing class ’ s members. A derived class normally adds its own fields and methods to represent a more specialized group of objects. Inheritance saves time by reusing proven and debugged high-quality software.

6  2009 Pearson Education, Inc. All rights reserved. 6 11.1 Introduction (Cont.) The direct base class is the base class which the derived class explicitly inherits. An indirect base class is any class above the direct base class in the class hierarchy. The class hierarchy begins with class object.

7  2009 Pearson Education, Inc. All rights reserved. 7 11.1 Introduction (Cont.) The is-a relationship represents inheritance. For example, a car is a vehicle, and a truck is a vehicle. New classes can inherit from thousands of pre-built classes in class libraries.

8  2009 Pearson Education, Inc. All rights reserved. 8 11.2 Base Classes and Derived Classes Figure 11.1 lists several simple examples of base classes and derived classes. Note that base classes are “more general,” and derived classes are “more specific.” Fig. 11.1 | Inheritance examples.

9  2009 Pearson Education, Inc. All rights reserved. 9 11.2 Base Classes and Derived Classes (Cont.) The UML class diagram of Fig. 11.2 shows an inheritance hierarchy representing a university community. Each arrow represents an is-a relationship. Fig. 11.2 | UML class diagram showing an inheritance hierarchy for university CommunityMember s.

10  2009 Pearson Education, Inc. All rights reserved. 10 11.2 Base Classes and Derived Classes (Cont.) Now consider the Shape inheritance hierarchy in Fig. 11.3. We can follow the arrows to identify several is-a relationships. Fig. 11.3 | UML class diagram showing an inheritance hierarchy for Shapes.

11  2009 Pearson Education, Inc. All rights reserved. 11 11.2 Base Classes and Derived Classes (Cont.) Objects of all classes that extend a common base class can be treated as objects of that base class. However, base-class objects cannot be treated as objects of their derived classes. When a derived class needs a customized version of an inherited method, the derived class can override the base-class method.

12  2009 Pearson Education, Inc. All rights reserved. 12 11.3 protected Members A base class’s private members are inherited by derived classes, but are not directly accessible by derived-class methods and properties. A base class’s protected members can be accessed by members of that base class and by members of its derived classes. A base class’s protected internal members can be accessed by members of a base class, the derived classes and by any class in the same assembly.

13  2009 Pearson Education, Inc. All rights reserved. 13 11.3 protected Members (Cont.) Software Engineering Observation 11.1 Properties and methods of a derived class cannot directlyaccess private members of the base class. A derived class can change the state of private base-class fields only through non -private methods and properties provided in the base class. Software Engineering Observation 11.2 If a derived class can access its base class’s private fields, classes that inherit from that base class could access the fields as well. This would propagate access to what should be private fields, and the benefits of information hiding would be lost.

14  2009 Pearson Education, Inc. All rights reserved. 14 Fig. 11.4 | CommissionEmployee class represents a commission employee. (Part 1 of 5.) Outline Commission Employee.cs (1 of 5 ) CommissionEmployee (Fig. 11.4) represents an employee who is paid a percentage of their sales. A colon ( : ) followed a class name at the end of the class declaration header indicates that the class extends the class to the right of the colon.

15  2009 Pearson Education, Inc. All rights reserved. 15 Fig. 11.4 | CommissionEmployee class represents a commission employee. (Part 2 of 5.) Outline Commission Employee.cs (2 of 5 )

16  2009 Pearson Education, Inc. All rights reserved. 16 Fig. 11.4 | CommissionEmployee class represents a commission employee. (Part 3 of 5.) Outline Commission Employee.cs (3 of 5 )

17  2009 Pearson Education, Inc. All rights reserved. 17 Fig. 11.4 | CommissionEmployee class represents a commission employee. (Part 4 of 5.) Outline Commission Employee.cs (4 of 5 )

18  2009 Pearson Education, Inc. All rights reserved. 18 Fig. 11.4 | CommissionEmployee class represents a commission employee. (Part 5 of 5.) Outline Commission Employee.cs (5 of 5 )

19  2009 Pearson Education, Inc. All rights reserved. 19 11.4 Relationship between Base Classes and Derived Classes (Cont.) A colon ( : ) followed a class name at the end of the class declaration header indicates that the class extends the class to the right of the colon. Every C# class directly or indirectly inherits object ’s methods. If a class does not specify that it inherits from another class, it implicitly inherits from object. Software Engineering Observation 11.3 The compiler sets the base class of a class to object when the class declaration does not explicitly extend a base class.

20  2009 Pearson Education, Inc. All rights reserved. 20 11.4 Relationship between Base Classes and Derived Classes (Cont.) Declaring instance variables as private and providing public properties to manipulate and validate them helps enforce good software engineering. Constructors are not inherited. Either explicitly or implicitly, a call to the base-class constructor is made. Class object ’s default (empty) constructor does nothing. Note that even if a class does not have constructors, the default constructor will call the base class’s default or parameterless constructor.

21  2009 Pearson Education, Inc. All rights reserved. 21 11.4 Relationship between Base Classes and Derived Classes (Cont.) Method ToString is special—it is one of the methods that every class inherits directly or indirectly from class object. Method ToString returns a string representing an object. Class object ’s ToString method is primarily a placeholder that typically should be overridden by a derived class. To override a base-class method, a derived class must declare a method with keyword override. The method must have the same signature (method name, number of parameters and parameter types) and return type as the base-class method.

22  2009 Pearson Education, Inc. All rights reserved. 22 11.4 Relationship between Base Classes and Derived Classes (Cont.) Common Programming Error 11.1 It is a compilation error to override a method with a different access modifier. If a public method could be overridden as a protected or private method, the derived-class objects would not respond to the same method calls as base-class objects.

23  2009 Pearson Education, Inc. All rights reserved. 23 Figure 11.5 tests class CommissionEmployee. Fig. 11.5 | Testing class CommissionEmployee. (Part 1 of 3.) Outline Commission EmployeeTest.cs (1 of 3 )

24  2009 Pearson Education, Inc. All rights reserved. 24 Fig. 11.5 | Testing class CommissionEmployee. (Part 2 of 3.) Outline Commission EmployeeTest.cs (2 of 3 )

25  2009 Pearson Education, Inc. All rights reserved. 25 Fig. 11.5 | Testing class CommissionEmployee. (Part 3 of 3.) Outline Commission EmployeeTest.cs (3 of 3 )

26  2009 Pearson Education, Inc. All rights reserved. 26 We now declare and test a separate class BasePlusCommissionEmployee (Fig. 11.6), Fig. 11.6 | BasePlusCommissionEmployee class represents an employee that receives a base salary in addition to a commission. (Part 1 of 6.) Outline BasePlus Commission Employee.cs (1 of 6 )

27  2009 Pearson Education, Inc. All rights reserved. 27 Fig. 11.6 | BasePlusCommissionEmployee class represents an employee that receives a base salary in addition to a commission. (Part 2 of 6.) Outline BasePlus Commission Employee.cs (2 of 6 )

28  2009 Pearson Education, Inc. All rights reserved. 28 Fig. 11.6 | BasePlusCommissionEmployee class represents an employee that receives a base salary in addition to a commission. (Part 3 of 6.) Outline BasePlus Commission Employee.cs (3 of 6 )

29  2009 Pearson Education, Inc. All rights reserved. 29 Fig. 11.6 | BasePlusCommissionEmployee class represents an employee that receives a base salary in addition to a commission. (Part 4 of 6.) Outline BasePlus Commission Employee.cs (4 of 6 )

30  2009 Pearson Education, Inc. All rights reserved. 30 Fig. 11.6 | BasePlusCommissionEmployee class represents an employee that receives a base salary in addition to a commission. (Part 5 of 6.) Outline BasePlus Commission Employee.cs (5 of 6 )

31  2009 Pearson Education, Inc. All rights reserved. 31 Fig. 11.6 | BasePlusCommissionEmployee class represents an employee that receives a base salary in addition to a commission. (Part 6 of 6.) Outline BasePlus Commission Employee.cs (6 of 6 )

32  2009 Pearson Education, Inc. All rights reserved. 32 11.4 Relationship between Base Classes and Derived Classes (Cont.) Note the similarity between this class and class Commission­Employee (Fig. 11.4)—in this example, we do not yet exploit that similarity.

33  2009 Pearson Education, Inc. All rights reserved. 33 Figure 11.7 tests class BasePlusCommissionEmployee. Fig. 11.7 | Testing class BasePlusCommissionEmployee. (Part 1 of 3.) Outline BasePlusCommission EmployeeTest.cs (1 of 3 )

34  2009 Pearson Education, Inc. All rights reserved. 34 Fig. 11.7 | Testing class BasePlusCommissionEmployee. (Part 2 of 3.) Outline BasePlusCommission EmployeeTest.cs (2 of 3 )

35  2009 Pearson Education, Inc. All rights reserved. 35 Fig. 11.7 | Testing class BasePlusCommissionEmployee. (Part 3 of 3.) Outline BasePlusCommission EmployeeTest.cs (3 of 3 )

36  2009 Pearson Education, Inc. All rights reserved. 36 11.4 Relationship between Base Classes and Derived Classes (Cont.) Much of the code for BasePlusCommissionEmployee is similar to the code for CommissionEmployee. Error-Prevention Tip 11.1 Copying and pasting code from one class to another can spread errors across multiple source-code files. Use inheritance rather than the “copy-and-paste” approach. Software Engineering Observation 11.4 With inheritance, the common members of all the classes in the hierarchy are declared in a base class. When changes are required for these common features, you need to make the changes only in the base class—derived classes then inherit the changes.

37  2009 Pearson Education, Inc. All rights reserved. 37 Now we declare class BasePlusCommissionEmployee (Fig. 11.8), which extends class CommissionEmployee (Fig. 11.4). Fig. 11.8 | BasePlusCommissionEmployee inherits from class CommissionEmployee. (Part 1 of 3.) Outline BasePlusCommission Employee.cs (1 of 3 ) Class BasePlusCommissionEmployee has an additional instance variable baseSalary. Invoke the CommissionEmployee ’s five-parameter constructor using a constructor initializer.

38  2009 Pearson Education, Inc. All rights reserved. 38 Fig. 11.8 | BasePlusCommissionEmployee inherits from class CommissionEmployee. (Part 2 of 3.) Outline BasePlusCommission Employee.cs (2 of 3 )

39  2009 Pearson Education, Inc. All rights reserved. 39 Fig. 11.8 | BasePlusCommissionEmployee inherits from class CommissionEmployee. (Part 3 of 3.) Outline BasePlusCommission Employee.cs (3 of 3 )

40  2009 Pearson Education, Inc. All rights reserved. 40 11.4 Relationship between Base Classes and Derived Classes (Cont.) A BasePlusCommissionEmployee object is a CommissionEmployee. A constructor initializer with keyword base invokes the base-class constructor. Common Programming Error 11.2 A compilation error occurs if a derived-class constructor calls one of its base-class constructors with arguments that do not match the number and types of parameters specified in one of the base-class constructor declarations.

41  2009 Pearson Education, Inc. All rights reserved. 41 11.4 Relationship between Base Classes and Derived Classes (Cont.) The virtual and abstract keywords indicate that a base-class method can be overridden in derived classes. The override modifier declares that a derived-class method overrides a virtual or abstract base-class method. This modifier also implicitly declares the derived-class method virtual. We need to declare CommissionEmployee ’ s Earnings method virtual.

42  2009 Pearson Education, Inc. All rights reserved. 42 11.4 Relationship between Base Classes and Derived Classes (Cont.) The compiler generates additional errors because base class CommissionEmployee ’s instance variables are private. The errors can be prevented by using the public properties inherited from class CommissionEmployee. Fig. 11.9 | Compilation errors generated by BasePlusCommissionEmployee (Fig. 11.8) after declaring the Earnings method in Fig. 11.4 with keyword virtual.

43  2009 Pearson Education, Inc. All rights reserved. 43 Class CommissionEmployee (Fig. 11.10) is modified to declare its instance variables as protected rather than private (Fig. 11.10). Fig. 11.10 | CommissionEmployee with protected instance variables. (Part 1 of 5.) Outline Commission Employee.cs ( 1 of 5 )

44  2009 Pearson Education, Inc. All rights reserved. 44 Fig. 11.10 | CommissionEmployee with protected instance variables. (Part 2 of 5.) Outline Commission Employee.cs ( 2 of 5 )

45  2009 Pearson Education, Inc. All rights reserved. 45 Fig. 11.10 | CommissionEmployee with protected instance variables. (Part 3 of 5.) Outline Commission Employee.cs ( 3 of 5 )

46  2009 Pearson Education, Inc. All rights reserved. 46 Fig. 11.10 | CommissionEmployee with protected instance variables. (Part 4 of 5.) Outline Commission Employee.cs ( 4 of 5 )

47  2009 Pearson Education, Inc. All rights reserved. 47 Fig. 11.10 | CommissionEmployee with protected instance variables. (Part 5 of 5.) We also declare the Earnings method virtual in line 78 so that BasePlusCommissionEmployee can override the method. Outline Commission Employee.cs ( 5 of 5 )

48  2009 Pearson Education, Inc. All rights reserved. 48 Class BasePlusCommissionEmployee (Fig. 11.11) is modified to extend CommissionEmployee. The instance variables are now protected members, so the compiler does not generate errors. Fig. 11.11 | BasePlusCommissionEmployee inherits from CommissionEmployee and has access to CommissionEmployee 's protected members. (Part 1 of 3.) Outline BasePlusCommissio nEmployee.cs ( 1 of 3 ) BasePlusCommissionEmplo yee ’s six-parameter constructor calls class CommissionEmployee ’s five- parameter constructor with a constructor initializer.

49  2009 Pearson Education, Inc. All rights reserved. 49 Fig. 11.11 | BasePlusCommissionEmployee inherits from CommissionEmployee and has access to CommissionEmployee 's protected members. (Part 2 of 3.) Outline BasePlusCommissio nEmployee.cs ( 2 of 3 )

50  2009 Pearson Education, Inc. All rights reserved. 50 Fig. 11.11 | BasePlusCommissionEmployee inherits from CommissionEmployee and has access to CommissionEmployee 's protected members. (Part 3 of 3.) Outline BasePlusCommissio nEmployee.cs ( 3 of 3 )

51  2009 Pearson Education, Inc. All rights reserved. 51 Figure 11.12 tests a BasePlusCommissionEmployee object. While the output is identical, there is less code repetition and overall this is a better implementation. Fig. 11.12 | Testing class BasePlusCommissionEmployee. (Part 1 of 3.) Outline BasePlusCommissio nEmployee.cs ( 1 of 3 )

52  2009 Pearson Education, Inc. All rights reserved. 52 Fig. 11.12 | Testing class BasePlusCommissionEmployee. (Part 2 of 3.) Outline BasePlusCommissio nEmployee.cs ( 2 of 3 )

53  2009 Pearson Education, Inc. All rights reserved. 53 Fig. 11.12 | Testing class BasePlusCommissionEmployee. (Part 3 of 3.) Outline BasePlusCommissio nEmployee.cs ( 3 of 3 )

54  2009 Pearson Education, Inc. All rights reserved. 54 11.4 Relationship between Base Classes and Derived Classes (Cont.) Software Engineering Observation 11.5 Declaring base-class instance variables private enables the base-class implementation of these instance variables to change without affecting derived-class implementations. Using protected instance variables creates several potential problems. – The derived-class object can set an inherited variable’s value directly without validity checking. – Derived-class methods would need to be written to depend on the base class’s data implementation. You should be able to change the base-class implementation while still providing the same services to the derived classes.

55  2009 Pearson Education, Inc. All rights reserved. 55 Class CommissionEmployee (Fig. 11.13) is modified to declare private instance variables and provide public properties. Fig. 11.13 | CommissionEmployee class represents a commission employee. (Part 1 of 5.) Outline Commission Employee.cs ( 1 of 5 )

56  2009 Pearson Education, Inc. All rights reserved. 56 Fig. 11.13 | CommissionEmployee class represents a commission employee. (Part 2 of 5.) Outline Commission Employee.cs ( 2 of 5 )

57  2009 Pearson Education, Inc. All rights reserved. 57 Fig. 11.13 | CommissionEmployee class represents a commission employee. (Part 3 of 5.) Outline Commission Employee.cs ( 3 of 5 )

58  2009 Pearson Education, Inc. All rights reserved. 58 Fig. 11.13 | CommissionEmployee class represents a commission employee. (Part 4 of 5.) Outline Commission Employee.cs ( 4 of 5 ) Use the class’s properties to obtain the values of its instance variables.

59  2009 Pearson Education, Inc. All rights reserved. 59 Fig. 11.13 | CommissionEmployee class represents a commission employee. (Part 5 of 5.) Outline Commission Employee.cs ( 5 of 5 )

60  2009 Pearson Education, Inc. All rights reserved. 60 Class BasePlusCommissionEmployee (Fig. 11.14) has several changes to its method implementations. Fig. 11.14 | BasePlusCommissionEmployee inherits from CommissionEmployee and has access to CommissionEmployee 's private data via its public properties. (Part 1 of 3.) Outline BasePlusCommissio nEmployee.cs ( 1 of 3 )

61  2009 Pearson Education, Inc. All rights reserved. 61 Fig. 11.14 | BasePlusCommissionEmployee inherits from CommissionEmployee and has access to CommissionEmployee 's private data via its public properties. (Part 2 of 3.) Outline BasePlusCommissio nEmployee.cs ( 2 of 3 )

62  2009 Pearson Education, Inc. All rights reserved. 62 Fig. 11.14 | BasePlusCommissionEmployee inherits from CommissionEmployee and has access to CommissionEmployee 's private data via its public properties. (Part 3 of 3.) Outline BasePlusCommissio nEmployee.cs ( 3 of 3 ) Use CommissionEmployee ’s Earnings method to calculate the commission pay, and add it to the BaseSalary.

63  2009 Pearson Education, Inc. All rights reserved. 63 11.4 Relationship between Base Classes and Derived Classes (Cont.) Common Programming Error 11.3 When a base-class method is overridden in a derived class, the derived-class version often calls the base-class version to do a portion of the work. Failure to prefix the base-class method name with the keyword base when referencing the base class’s method causes the derived-class method to call itself. Common Programming Error 11.4 The use of “chained” base references to refer to a member several levels up the hierarchy—as in base.base.Earnings() —is a compilation error.

64  2009 Pearson Education, Inc. All rights reserved. 64 Figure 11.15 performs the same manipulations on a BasePlusCommissionEmployee object. By using inheritance and properties, we have efficiently and effectively constructed a well-engineered class. Fig. 11.15 | Testing class BasePlusCommissionEmployee4. (Part 1 of 3.) Outline BasePlusCommissio nEmployeeTest.cs ( 1 of 3 )

65  2009 Pearson Education, Inc. All rights reserved. 65 Fig. 11.15 | Testing class BasePlusCommissionEmployee4. (Part 2 of 3.) Outline BasePlusCommissio nEmployeeTest.cs ( 2 of 3 )

66  2009 Pearson Education, Inc. All rights reserved. 66 Fig. 11.15 | Testing class BasePlusCommissionEmployee4. (Part 3 of 3.) Outline BasePlusCommissio nEmployeeTest.cs ( 3 of 3 )

67  2009 Pearson Education, Inc. All rights reserved. 67 11.5 Constructors in Derived Classes The derived-class constructor, before performing its own tasks, invokes its direct base class’s constructor. This is done either explicitly or implicitly. If the base class is derived from another class, the base-class constructor invokes the constructor of the next class up in the hierarchy, and so on.

68  2009 Pearson Education, Inc. All rights reserved. 68 11.5 Constructors in Derived Classes (Cont.) Software Engineering Observation 11.6 When an application creates a derived-class object, the derived-class constructor calls the base-class constructor (explicitly, via base, or implicitly). The base-class constructor’s body executes to initialize the base class’s instance variables that are part of the derived-class object, then the derived class constructor’s body executes to initialize the derived-class-only instance variables. Even if a constructor does not assign a value to an instance variable, the variable is still initialized to its default value.

69  2009 Pearson Education, Inc. All rights reserved. 69 Class CommissionEmployee (Fig. 11.16) is modified to output text when its constructor is invoked. Fig. 11.16 | CommissionEmployee class represents a commission employee. (Part 1 of 5.) Outline Commission Employee.cs ( 1 of 5 )

70  2009 Pearson Education, Inc. All rights reserved. 70 Fig. 11.16 | CommissionEmployee class represents a commission employee. (Part 2 of 5.) Outline Commission Employee.cs ( 2 of 5 )

71  2009 Pearson Education, Inc. All rights reserved. 71 Fig. 11.16 | CommissionEmployee class represents a commission employee. (Part 3 of 5.) Outline Commission Employee.cs ( 3 of 5 )

72  2009 Pearson Education, Inc. All rights reserved. 72 Fig. 11.16 | CommissionEmployee class represents a commission employee. (Part 4 of 5.) Outline Commission Employee.cs ( 4 of 5 )

73  2009 Pearson Education, Inc. All rights reserved. 73 Fig. 11.16 | CommissionEmployee class represents a commission employee. (Part 5 of 5.) Outline Commission Employee.cs ( 5 of 5 )

74  2009 Pearson Education, Inc. All rights reserved. 74 Class BasePlusCommissionEmployee (Fig. 11.17) is modified to output text when its constructor is invoked. Fig. 11.17 | BasePlusCommissionEmployee class declaration. (Part 1 of 3.) Outline BasePlusCommissio nEmployee.cs ( 1 of 3 )

75  2009 Pearson Education, Inc. All rights reserved. 75 Fig. 11.17 | BasePlusCommissionEmployee class declaration. (Part 2 of 3.) Outline BasePlusCommissio nEmployee.cs ( 2 of 3 )

76  2009 Pearson Education, Inc. All rights reserved. 76 Fig. 11.17 | BasePlusCommissionEmployee class declaration. (Part 3 of 3.) Outline BasePlusCommissio nEmployee.cs ( 3 of 3 )

77  2009 Pearson Education, Inc. All rights reserved. 77 Figure 11.18 demonstrates the order in which constructors are called in an inheritance hierarchy. Fig. 11.18 | Display order in which base-class and derived-class constructors are called. (Part 1 of 3.) Outline Constructor Test.cs ( 1 of 3 )

78  2009 Pearson Education, Inc. All rights reserved. 78 Fig. 11.18 | Display order in which base-class and derived-class constructors are called. (Part 2 of 3.) Outline Constructor Test.cs ( 2 of 3 )

79  2009 Pearson Education, Inc. All rights reserved. 79 Fig. 11.18 | Display order in which base-class and derived-class constructors are called. (Part 3 of 3.) Outline Constructor Test.cs ( 3 of 3 )

80  2009 Pearson Education, Inc. All rights reserved. 80 11.6 Software Engineering with Inheritance When a new class extends an existing class, the new class inherits the members of the existing class. We can customize the new class to meet our needs by including additional members and by overriding base-class members. Independent software vendors (ISVs) can develop and sell proprietary classes. Users then can derive new classes from these library classes without accessing the source code.

81  2009 Pearson Education, Inc. All rights reserved. 81 11.6 Software Engineering with Inheritance (Cont.) Software Engineering Observation 11.7 Although inheriting from a class does not require access to the class’s source code, developers often insist on seeing the source code to understand how the class is implemented. They may, for example, want to ensure that they are extending a class that performs well and is implemented securely. Effective software reuse greatly improves the software- development process. Object-oriented programming facilitates software reuse. The availability of class libraries delivers the maxi­mum benefits of software reuse.

82  2009 Pearson Education, Inc. All rights reserved. 82 11.6 Software Engineering with Inheritance (Cont.) Software Engineering Observation 11.8 At the design stage in an object-oriented system, the designer often finds that certain classes are closely related. The designer should “factor out” common members and place them in a base class. Software Engineering Observation 11.9 Declaring a derived class does not affect its base class’s source code. Inheritance preserves the in­tegrity of the base class.

83  2009 Pearson Education, Inc. All rights reserved. 83 11.6 Software Engineering with Inheritance (Cont.) Software Engineering Observation 11.10 Designers of object-oriented systems should avoid class proliferation. Such proliferation creates management problems and can hinder software reusability, because in a huge class library it becomes difficult for a client to locate the most appropriate classes. Performance Tip 11.1 If derived classes are larger than they need to be (i.e., contain too much functionality), memory and pro­cessing resources might be wasted. Extend the base class containing the functionality that is closest to what is needed.

84  2009 Pearson Education, Inc. All rights reserved. 84 11.7 Class object All classes inherit directly or indirectly from the object class. Figure 11.19 summarizes object ’s methods. Fig. 11.19 | object methods that are inherited directly or indirectly by all classes. (Part 1 of 2.)

85  2009 Pearson Education, Inc. All rights reserved. 85 Fig. 11.19 | object methods that are inherited directly or indirectly by all classes. (Part 2 of 2.) 11.7 Class object (Cont.)


Download ppt " 2009 Pearson Education, Inc. All rights reserved. 1 11 Object-Oriented Programming: Inheritance."

Similar presentations


Ads by Google