1 4 ObjectOriented Programming Inheritance 1992 2007 Pearson
1 4 Object-Oriented Programming: Inheritance 1992 -2007 Pearson Education, Inc. All rights reserved.
2 OBJECTIVES In this chapter you will learn: § How inheritance promotes software reusability. § The notions of superclasses and subclasses. § To use keyword extends to create a class that inherits attributes and behaviors from another class. § To use access modifier protected to give subclass methods access to superclass members. § To access superclass members with super. § How constructors are used in inheritance hierarchies. § The methods of class Object, the direct or indirect superclass of all classes in Java. 1992 -2007 Pearson Education, Inc. All rights reserved.
3 9. 1 Introduction 9. 2 Superclasses and Subclasses 9. 3 protected Members 9. 4 Relationship between Superclasses and Subclasses 9. 4. 1 Creating and Using a Commission. Employee Class 9. 4. 2 Creating a Base. Plus. Commission. Employee Class without Using Inheritance Creating a Commission. Employee– Base. Plus. Commission. Employee Inheritance Hierarchy Using protected Instance Variables Commission. Employee– Base. Plus. Commission. Employee Inheritance Hierarchy Using private Instance Variables 9. 4. 3 9. 4. 4 9. 4. 5 1992 -2007 Pearson Education, Inc. All rights reserved.
4 9. 5 Constructors in Subclasses 9. 6 Software Engineering with Inheritance 9. 7 Object Class 9. 8 (Optional) GUI and Graphics Case Study: Displaying Text and Images Using Labels 9. 9 Wrap-Up 1992 -2007 Pearson Education, Inc. All rights reserved.
5 4. 1 Introduction • Inheritance – Software reusability – Create new class from existing class • Absorb existing class’s data and behaviors • Enhance with new capabilities – Subclass extends superclass • Subclass – More specialized group of objects – Behaviors inherited from superclass • Can customize – Additional behaviors 1992 -2007 Pearson Education, Inc. All rights reserved.
6 4. 1 Introduction (Cont. ) • Class hierarchy – Direct superclass • Inherited explicitly (one level up hierarchy) – Indirect superclass • Inherited two or more levels up hierarchy – Single inheritance • Inherits from one superclass – Multiple inheritance • Inherits from multiple superclasses – Java does not support multiple inheritance 1992 -2007 Pearson Education, Inc. All rights reserved.
7 4. 2 Superclasses and subclasses • Superclasses and subclasses – Object of one class “is an” object of another class • Example: Rectangle is quadrilateral. – Class Rectangle inherits from class Quadrilateral – Quadrilateral: superclass – Rectangle: subclass – Superclass typically represents larger set of objects than subclasses • Example: – superclass: Vehicle • Cars, trucks, boats, bicycles, … – subclass: Car • Smaller, more-specific subset of vehicles 1992 -2007 Pearson Education, Inc. All rights reserved.
8 Fig. 9. 1 | Inheritance examples. 1992 -2007 Pearson Education, Inc. All rights reserved.
9 4. 2 Superclasses and subclasses (Cont. ) • Inheritance hierarchy – Inheritance relationships: tree-like hierarchy structure – Each class becomes • superclass – Supply members to other classes OR • subclass – Inherit members from other classes 1992 -2007 Pearson Education, Inc. All rights reserved.
10 Fig. 9. 2 | Inheritance hierarchy for university Community. Members 1992 -2007 Pearson Education, Inc. All rights reserved.
11 Fig. 9. 3 | Inheritance hierarchy for Shapes. 1992 -2007 Pearson Education, Inc. All rights reserved.
12 4. 3 protected Members • protected access – Intermediate level of protection between public and private – protected members accessible by • superclass members • subclass members • Class members in the same package – Subclass access to superclass member • Keyword super and a dot (. ) 1992 -2007 Pearson Education, Inc. All rights reserved.
13 Software Engineering Observation 4. 1 Methods of a subclass cannot directly access private members of their superclass. A subclass can change the state of private superclass instance variables only through non-private methods provided in the superclass and inherited by the subclass. 1992 -2007 Pearson Education, Inc. All rights reserved.
14 Software Engineering Observation 4. 2 Declaring private instance variables helps programmers test, debug and correctly modify systems. If a subclass could access its superclass’s private instance variables, classes that inherit from that subclass could access the instance variables as well. This would propagate access to what should be private instance variables, and the benefits of information hiding would be lost. 1992 -2007 Pearson Education, Inc. All rights reserved.
4. 4 Relationship between Superclasses and Subclasses 15 • Superclass and subclass relationship – Example: Commission. Employee/Base. Plus. Commission. Employee inheritance hierarchy • Commission. Employee – First name, last name, SSN, commission rate, gross sale amount • Base. Plus. Commission. Employee – First name, last name, SSN, commission rate, gross sale amount – Base salary 1992 -2007 Pearson Education, Inc. All rights reserved.
4. 4 Relationship between Superclasses and Subclasses 16 • Superclass and subclass relationship – Example: Commission. Employee/Base. Plus. Commission. Employee inheritance hierarchy • Commission. Employee – First name, last name, SSN, commission rate, gross sale amount – Get commision from sales – Earnings = gross. Sales*commission rate • Base. Plus. Commission. Employee – First name, last name, SSN, commission rate, gross sale amount – Base salary – They have a base salary + get commissions from sales 1992 -2007 Pearson Education, Inc. All rights reserved.
4. 4 Relationship between Superclasses and Subclasses 17 • Commission. Employee • Instance variables – first name String – commission rate, gross sale amount: double • valideted • Methods – 3 parameter constructor – get and set methods for the 3 instance variables – to. String method to return a string representation of the objcet – A method to calculate the revenue 1992 -2007 Pearson Education, Inc. All rights reserved.
4. 4. 1 Creating and Using a Commission. Employee Class 18 • Class Commission. Employee – Extends class Object • Keyword extends • Every class in Java extends an existing class – Except Object • Every class inherits Object’s methods • New class implicitly extends Object – If it does not extend another class 1992 -2007 Pearson Education, Inc. All rights reserved.
19 Software Engineering Observation 4. 3 The Java compiler sets the superclass of a class to Object when the class declaration does not explicitly extend a superclass. 1992 -2007 Pearson Education, Inc. All rights reserved.
Declare private instance variables Outline 20 Class Commission. Employee extends class Object Commission. Employee. java Implicit call to Object constructor Initialize instance variables (1 of 4) Line 4 Lines 6 -10 Line 16 Invoke methods set. Gross. Sales and set. Commission. Rate to Lines validate data 17 -21 Lines 20 -21 1992 -2007 Pearson Education, Inc. All rights reserved.
Outline 21 Commission. Employee. java (2 of 4) 1992 -2007 Pearson Education, Inc. All rights reserved.
Outline 22 Commission. Employee. java (3 of 4) Lines 85 -88 Calculate earnings 1992 -2007 Pearson Education, Inc. All rights reserved.
Outline 23 Override method to. String of class Object Commission. Employee. java (4 of 4) Lines 91 -98 1992 -2007 Pearson Education, Inc. All rights reserved.
24 Class Commission. Employee 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 public class Commission. Employee extends Object { private String first. Name; private String last. Name; private String social. Security. Number; private double gross. Sales; // gross weekly sales private double commission. Rate; // commission percentage // five-argument constructor public Commission. Employee( String first, String last, String ssn, double sales, double rate ) { // implicit call to Object constructor occurs here first. Name = first; last. Name = last; social. Security. Number = ssn; set. Gross. Sales( sales ); // validate and store gross sales set. Commission. Rate( rate ); // validate and store commission rate } // end five-argument Commission. Employee constructor 1992 -2007 Pearson Education, Inc. All rights reserved.
25 Class Commission. Employee (cont) public class Commission. Employee extends Object { private String first. Name; private double gross. Sales; // gross weekly sales private double commission. Rate; // commission percentage // five-argument constructor public Commission. Employee( String first, String last, String ssn, double sales, double rate ) { // implicit call to Object constructor occurs here first. Name = first; last. Name = last; social. Security. Number = ssn; set. Gross. Sales( sales ); // validate and store gross sales set. Commission. Rate( rate ); // validate and store commission rate } // end five-argument Commission. Employee constructor 1992 -2007 Pearson Education, Inc. All rights reserved.
26 Class Commission. Employee (cont) 24 25 26 27 28 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 // set first name public void set. First. Name( String first ) { first. Name = first; } // end method set. First. Na // return first name public String get. First. Name() { return first. Name; } // end method get. First. Name // set last name public void set. Last. Name( String last ) { last. Name = last; } // end method set. Last. Name // return last name public String get. Last. Name() { return last. Name; } // end method get. Last. Name 1992 -2007 Pearson Education, Inc. All rights reserved.
27 Class Commission. Employee (cont) 48 49 50 51 52 53 54 55 56 57 58 60 61 62 63 64 65 66 67 68 69 70 71 // set social security number public void set. Social. Security. Number( String ssn ) { social. Security. Number = ssn; // should validate } // end method set. Social. Security. Number // return social security number public String get. Social. Security. Number() { return social. Security. Number; } // end method get. Social. Security. Number // set gross sales amount public void set. Gross. Sales( double sales ) { gross. Sales = ( sales < 0. 0 ) ? 0. 0 : sales; } // end method set. Gross. Sales // return gross sales amount public double get. Gross. Sales() { return gross. Sales; } // end method get. Gross. Sales 1992 -2007 Pearson Education, Inc. All rights reserved.
28 Class Commission. Employee (cont) 78 // return commission rate 79 public double get. Commission. Rate() 80 { 81 return commission. Rate; 82 } // end method get. Commission. Rate 72 // set commission rate 73 public void set. Commission. Rate( double rate ) 74 { 75 commission. Rate = ( rate > 0. 0 && rate < 1. 0 ) ? rate : 0. 0; 76 } // end method set. Commission. Rate 84 85 86 87 88 // calculate earnings public double earnings() { return commission. Rate * gross. Sales; } // end method earnings 1992 -2007 Pearson Education, Inc. All rights reserved.
29 Class Commission. Employee (cont) 90 91 92 93 94 95 96 97 98 99 // return String representation of Commission. Employee object public String to. String() { return String. format( "%s: %s %sn%s: %. 2 fn%s: %. 2 f", "commission employee", first. Name, last. Name, "social security number", social. Security. Number, "gross sales", gross. Sales, "commission rate", commission. Rate ); } // end method to. String } // end class Commission. Employee 1992 -2007 Pearson Education, Inc. All rights reserved.
30 Common Programming Error 4. 1 It is a syntax error to override a method with a more restricted access modifier—a public method of the superclass cannot become a protected or private method in the subclass; a protected method of the superclass cannot become a private method in the subclass. Doing so would break the “is-a” relationship in which it is required that all subclass objects be able to respond to method calls that are made to public methods declared in the superclass. (cont…) 1992 -2007 Pearson Education, Inc. All rights reserved.
31 Common Programming Error 4. 1 If a public method could be overridden as a protected or private method, the subclass objects would not be able to respond to the same method calls as superclass objects. Once a method is declared public in a superclass, the method remains public for all that class’s direct and indirect subclasses. 1992 -2007 Pearson Education, Inc. All rights reserved.
Outline 32 Instantiate Commission. Employee object Commission. Employee Test. java (1 of 2) Lines 9 -10 Lines 15 -25 Use Commission. Employee’s get methods Line 26 -27 to retrieve the object’s instance variable values Use Commission. Employee’s set methods to change the object’s instance variable values 1992 -2007 Pearson Education, Inc. All rights reserved.
Outline 33 Implicitly call object’s to. String method Commission. Employee Test. java (2 of 2) Line 30 Program output 1992 -2007 Pearson Education, Inc. All rights reserved.
Test class Commission. Employee. Test 34 The test class creates an Commission. Employee object and Print its characteristics using get methods Modify its gross. Sales using the set. Gross. Sales method Print the objcet using the to. String method 1992 -2007 Pearson Education, Inc. All rights reserved.
Test class Commission. Employee. Test 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 35 public class Commission. Employee. Test { public static void main( String args[] ) { // instantiate Commission. Employee object Commission. Employee employee = new Commission. Employee( "Sue", "Jones", "222 -22 -2222", 10000, . 06 ); // get commission employee data System. out. println( "Employee information obtained by get methods: n" ); System. out. printf( "%s %sn", "First name is", employee. get. First. Name() ); System. out. printf( "%s %sn", "Last name is", employee. get. Last. Name() ); System. out. printf( "%s %sn", "Social security number is", employee. get. Social. Security. Number() ); System. out. printf( "%s %. 2 fn", "Gross sales is", employee. get. Gross. Sales() ); System. out. printf( "%s %. 2 fn", "Commission rate is", employee. get. Commission. Rate() ); 1992 -2007 Pearson Education, Inc. All rights reserved.
Test class Commission. Employee. Test (cont) 26 sales 27 commission 28 29 36 employee. set. Gross. Sales( 500 ); // set gross employee. set. Commission. Rate(. 1 ); // set rate System. out. printf( "n%s: nn%sn", 30 "Updated employee information obtained by to. String", employee ); 31 } // end main 32 } // end class Commission. Employee. Test 1992 -2007 Pearson Education, Inc. All rights reserved.
37 output-Commission. Employee. Test Employee information obtained by get methods: First name is Sue Last name is Jones Social security number is 222 -22 -2222 Gross sales is 10000. 00 Commission rate is 0. 06 Updated employee information obtained by to. String: commission employee: Sue Jones social security number: 222 -22 -2222 gross sales: 500. 00 commission rate: 0. 10 1992 -2007 Pearson Education, Inc. All rights reserved.
38 4. 4. 2 Creating a Base. Plus. Commission. Employee Class without Using Inheritance • Class Base. Plus. Commission. Employee – Implicitly extends Object – Much of the code is similar to Commission. Employee • private instance variables • public methods • constructor – Additions • private instance variable base. Salary • Methods set. Base. Salary and get. Base. Salary 1992 -2007 Pearson Education, Inc. All rights reserved.
Outline 39 Base. Plus. Commission Employee. java Add instance variable base. Salary (1 of 4) Line 12 Line 24 Use method set. Base. Salary to validate data 1992 -2007 Pearson Education, Inc. All rights reserved.
Outline 40 Base. Plus. Commission Employee. java (2 of 4) 1992 -2007 Pearson Education, Inc. All rights reserved.
Outline 41 Base. Plus. Commission Employee. java (3 of 4) 1992 -2007 Pearson Education, Inc. All rights reserved.
Outline Method set. Base. Salary validates data and sets instance variable base. Salary 42 Base. Plus. Commission Employee. java (4 of 4) Lines 88 -91 Method get. Base. Salary returns the value of instance variable base. Salary Lines 94 -97 Line 102 Lines 108 -113 Update method earnings to calculate the earnings of a base-salaried commission employee Update method to. String to display base salary 1992 -2007 Pearson Education, Inc. All rights reserved.
Outline 43 Instantiate Base. Plus. Commission. Employee object. Base. Plus. Commission Employee. Test. java (1 of 2) Line 9 -11 Lines 16 -27 Use Base. Plu. Commission. Employee’s get methods to retrieve the object’s instance variable values 1992 -2007 Pearson Education, Inc. All rights reserved.
Outline 44 Use Base. Plus. Commission. Employee’s set. Base. Salary methods to set base salary Explicitly call object’s to. String method Base. Plus. Commission Employee. Test. java (2 of 2) Line 29 Line 33 Program output 1992 -2007 Pearson Education, Inc. All rights reserved.
45 Class Base. Plus. Commission. Employee 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 public class Base. Plus. Commission. Employee { private String first. Name; private String last. Name; private String social. Security. Number; private double gross. Sales; // gross weekly sales private double commission. Rate; // commission percentage private double base. Salary; // base salary per week // six-argument constructor public Base. Plus. Commission. Employee( String first, String last, String ssn, double sales, double rate, double salary ) { // implicit call to Object constructor occurs here first. Name = first; last. Name = last; social. Security. Number = ssn; set. Gross. Sales( sales ); // validate and store gross sales set. Commission. Rate( rate ); // validate and store commission rate set. Base. Salary( salary ); // validate and store base salary } // end six-argument Base. Plus. Commission. Employee constructor 1992 -2007 Pearson Education, Inc. All rights reserved.
Class Base. Plus. Commission. Employee (cont) 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 46 // set first name public void set. First. Name( String first ) { first. Name = first; } // end method set. First. Name // return first name public String get. First. Name() { return first. Name; } // end method get. First. Name // set last name public void set. Last. Name( String last ) { last. Name = last; } // end method set. Last. Name // return last name public String get. Last. Name() { return last. Name; } // end method get. Last. Name 1992 -2007 Pearson Education, Inc. All rights reserved.
Class Base. Plus. Commission. Employee (cont) 51 52 53 54 55 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 47 // set social security number public void set. Social. Security. Number( String ssn ) { social. Security. Number = ssn; // should validate } // end method set. Social. Security. Number // return social security number public String get. Social. Security. Number() { return social. Security. Number; } // end method get. Social. Security. Number // set gross sales amount public void set. Gross. Sales( double sales ) { gross. Sales = ( sales < 0. 0 ) ? 0. 0 : sales; } // end method set. Gross. Sales // return gross sales amount public double get. Gross. Sales() { return gross. Sales; } // end method get. Gross. Sales 1992 -2007 Pearson Education, Inc. All rights reserved.
Class Base. Plus. Commission. Employee (cont) 75 76 77 78 79 80 81 82 83 84 85 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 48 // set commission rate public void set. Commission. Rate( double rate ) { commission. Rate = ( rate > 0. 0 && rate < 1. 0 ) ? rate : 0. 0; } // end method set. Commission. Rate // return commission rate public double get. Commission. Rate() { return commission. Rate; } // end method get. Commission. Rate // set base salary public void set. Base. Salary( double salary ) { base. Salary = ( salary < 0. 0 ) ? 0. 0 : salary; } // end method set. Base. Salary // return base salary public double get. Base. Salary() { return base. Salary; } // end method get. Base. Salary // calculate earnings public double earnings() { return base. Salary + ( commission. Rate * gross. Sales ); } // end method earnings 1992 -2007 Pearson Education, Inc. All rights reserved.
Class Base. Plus. Commission. Employee (cont) 105 106 107 108 109 110 111 112 49 // return String representation of Base. Plus. Commission. Employee public String to. String() { return String. format( "%s: %s %sn%s: %. 2 fn%s: %. 2 f", "base-salaried commission employee", first. Name, last. Name, "social security number", social. Security. Number, "gross sales", gross. Sales, "commission rate", commission. Rate, 113 "base salary", base. Salary ); 114 } // end method to. String 115 } // end class Base. Plus. Commission. Employee 1992 -2007 Pearson Education, Inc. All rights reserved.
Test class Base. Plus. Commission. Employee. Test 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 50 public class Base. Plus. Commission. Employee. Test { public static void main( String args[] ) { // instantiate Base. Plus. Commission. Employee object Base. Plus. Commission. Employee employee = new Base. Plus. Commission. Employee( "Bob", "Lewis", "333 -33 -3333", 5000, . 04, 300 ); // get base-salaried commission employee data System. out. println( "Employee information obtained by get methods: n" ); System. out. printf( "%s %sn", "First name is", employee. get. First. Name() ); System. out. printf( "%s %sn", "Last name is", employee. get. Last. Name() ); System. out. printf( "%s %sn", "Social security number is", employee. get. Social. Security. Number() ); System. out. printf( "%s %. 2 fn", "Gross sales is", employee. get. Gross. Sales() ); System. out. printf( "%s %. 2 fn", "Commission rate is", employee. get. Commission. Rate() ); System. out. printf( "%s %. 2 fn", "Base salary is", employee. get. Base. Salary() ); 1992 -2007 Pearson Education, Inc. All rights reserved.
Test class Base. Plus. Commission. Employee. Test (cont) 51 29 employee. set. Base. Salary( 1000 ); // set base salary 30 31 System. out. printf( "n%s: nn%sn", 32 "Updated employee information obtained by to. String", 33 employee. to. String() ); 34 } // end main 35 } // end class Base. Plus. Commission. Employee. Test 1992 -2007 Pearson Education, Inc. All rights reserved.
52 output Employee information obtained by get methods: First name is Bob Last name is Lewis Social security number is 333 -33 -3333 Gross sales is 5000. 00 Commission rate is 0. 04 Base salary is 300. 00 Updated employee information obtained by to. String: base-salaried commission employee: Bob Lewis social security number: 333 -33 -3333 gross sales: 5000. 00 commission rate: 0. 04 base salary: 1000. 00 1992 -2007 Pearson Education, Inc. All rights reserved.
53 Software Engineering Observation 4. 4 Copying and pasting code from one class to another can spread errors across multiple source code files. To avoid duplicating code (and possibly errors), use inheritance, rather than the “copyand-paste” approach, in situations where you want one class to “absorb” the instance variables and methods of another class. 1992 -2007 Pearson Education, Inc. All rights reserved.
54 Software Engineering Observation 4. 5 With inheritance, the common instance variables and methods of all the classes in the hierarchy are declared in a superclass. When changes are required for these common features, software developers need only to make the changes in the superclass—subclasses then inherit the changes. Without inheritance, changes would need to be made to all the source code files that contain a copy of the code in question. 1992 -2007 Pearson Education, Inc. All rights reserved.
55 4. 4. 3 Creating a Commission. Employee. Base. Plus. Commiion. Employee Inheritance Hierarchy • Class Base. Plus. Commission. Employee 2 – – – Extends class Commission. Employee Is a Commission. Employee Has instance variable base. Salary Inherits public and protected members Constructor not inherited 1992 -2007 Pearson Education, Inc. All rights reserved.
Outline 56 Base. Plus. Commission Employee 2. java Class Base. Plu. Commission. Employee 2 is a subclass of Commission. Employee (1 of 3) Line 4 Line 13 Invoke the superclass constructor using the superclass constructor call syntax 1992 -2007 Pearson Education, Inc. All rights reserved.
Outline 57 Base. Plus. Commission Compiler generates errors because superclass’s Employee 2. java instance variable commission. Rate and gross. Sales are private (2 of 3) Line 34 Lines 41 -46 Compiler generates errors because superclass’s instance variable first. Name, last. Name, social. Security. Number, gross. Sales and commission. Rate are private 1992 -2007 Pearson Education, Inc. All rights reserved.
Outline 58 Base. Plus. Commission Employee 2. java (3 of 3) Compiler generated errorss 1992 -2007 Pearson Education, Inc. All rights reserved.
Class Base. Plus. Commission. Employee 2 59 4 public class Base. Plus. Commission. Employee 2 extends Commission. Employee 5 { private double base. Salary; // base salary per week 7 8 // six-argument constructor 9 public Base. Plus. Commission. Employee 2( String first, String last, String ssn, double sales, double rate, double salary ) 11 { 12 // explicit call to superclass Commission. Employee constructor 13 super( first, last, ssn, sales, rate ); 14 15 set. Base. Salary( amount ); // validate and store base salary 16 } // end six-argument Base. Plus. Commission. Employee 2 constructor 17 1992 -2007 Pearson Education, Inc. All rights reserved.
Class Base. Plus. Commission. Employee 2 (cont) 18 19 20 21 22 60 // set base salary public void set. Base. Salary( double salary ) { base. Salary = ( salary < 0. 0 ) ? 0. 0 : salary; } // end method set. Base. Salary 24 // return base salary 25 public double get. Base. Salary() 26 { 27 return base. Salary; 28 } // end method get. Base. Salary 29 30 // calculate earnings 31 public double earnings() 32 { 33 // not allowed: commission. Rate and gross. Sales private in superclass 34 return base. Salary + ( commission. Rate * gross. Sales ); 35 } // end method earnings 1992 -2007 Pearson Education, Inc. All rights reserved.
Class Base. Plus. Commission. Employee 2 (cont) 37 38 39 40 41 // return String representation of Base. Plus. Commission. Employee 2 public String to. String() { // not allowed: attempts to access private superclass members return String. format( 42 "%s: %s %sn%s: %. 2 fn%s: %. 2 f" , 43 "base-salaried commission employee", first. Name, last. Name, 44 "social security number", social. Security. Number, 45 "gross sales", gross. Sales, "commission rate", commission. Rate, 46 "base salary", base. Salary ); 47 48 61 } // end method to. String } // end class Base. Plus. Commission. Employee 2 1992 -2007 Pearson Education, Inc. All rights reserved.
62 putput Base. Plus. Commission. Employee 2. java: 34: commission. Rate has private access in Commission. Employee return base. Salary + ( commission. Rate * gross. Sales ); ^ Base. Plus. Commission. Employee 2. java: 34: gross. Sales has private access in Commission. Employee return base. Salary + ( commission. Rate * gross. Sales ); ^ Base. Plus. Commission. Employee 2. java: 43: first. Name has private access in Commission. Employee "base-salaried commission employee", first. Name, last. Name, ^ Base. Plus. Commission. Employee 2. java: 43: last. Name has private access in Commission. Employee "base-salaried commission employee", first. Name, last. Name, ^ Base. Plus. Commission. Employee 2. java: 44: social. Security. Number has private access in Commission. Employee "social security number", social. Security. Number, ^ Base. Plus. Commission. Employee 2. java: 45: gross. Sales has private access in Commission. Employee "gross sales", gross. Sales, "commission rate", commission. Rate, ^ Base. Plus. Commission. Employee 2. java: 45: commission. Rate has private access in Commission. Employee "gross sales", gross. Sales, "commission rate", commission. Rate, ^ 7 errors 1992 -2007 Pearson Education, Inc. All rights reserved.
63 Common Programming Error 9. 2 A compilation error occurs if a subclass constructor calls one of its superclass constructors with arguments that do not match exactly the number and types of parameters specified in one of the superclass constructor declarations. 1992 -2007 Pearson Education, Inc. All rights reserved.
4. 4. 4 Commission. Employee. Base. Plus. Commission. Employee Inheritance Hierarchy Using protected Instance Variables 64 • Use protected instance variables – Enable class Base. Plus. Commission. Employee to directly access superclass instance variables – Superclass’s protected members are inherited by all subclases of that superclass 1992 -2007 Pearson Education, Inc. All rights reserved.
Outline Declare protected instance variables 65 Commission Employee 2. java (1 of 4) Line 6 -10 1992 -2007 Pearson Education, Inc. All rights reserved.
Outline 66 Commission Employee 2. java (2 of 4) 1992 -2007 Pearson Education, Inc. All rights reserved.
Outline 67 Commission Employee 2. java (3 of 4) 1992 -2007 Pearson Education, Inc. All rights reserved.
Outline 68 Commission Employee 2. java (4 of 4) 1992 -2007 Pearson Education, Inc. All rights reserved.
Outline 69 Base. Plus. Commission Employee 3. java Must call superclass’s (1 of 2) constructor Line 13 1992 -2007 Pearson Education, Inc. All rights reserved.
Outline 70 Base. Plus. Commission Employee 3. java Directly access superclass’s protected (2 of 2) instance variables Line 32 Lines 38 -43 1992 -2007 Pearson Education, Inc. All rights reserved.
Outline 71 Base. Plus. Commission Employee. Test 3. java (1 of 2) 1992 -2007 Pearson Education, Inc. All rights reserved.
Outline 72 Base. Plus. Commission Employee. Test 3. java (2 of 2) Program output 1992 -2007 Pearson Education, Inc. All rights reserved.
73 Class Commission. Employee public class Commission. Employee extends Object // optional { protected String first. Name; protected double gross. Sales; // gross weekly sales protected double commission. Rate; // commission percentage // five-argument constructor public Commission. Employee( String first, double sales, double rate ) { // implicit call to Object constructor occurs here first. Name = first; set. Gross. Sales( sales ); // validate and store gross sales set. Commission. Rate( rate ); // validate and store commission rate } // end Commission. Employee constructor 1992 -2007 Pearson Education, Inc. All rights reserved.
74 Class Commission. Employee (cont) // set first name public void set. First. Name( String first ) { first. Name = first; } // end method set. First. Name 30 // return first name public String get. First. Name() { return first. Name; } // end method get. First. Name 1992 -2007 Pearson Education, Inc. All rights reserved.
75 Class Commission. Employee (cont) // set gross sales amount public void set. Gross. Sales( double sales ) { gross. Sales = ( sales < 0. 0 ) ? 0. 0 : sales; } // end method set. Gross. Sales // return gross sales amount public double get. Gross. Sales() { return gross. Sales; } // end method get. Gross. Sales 1992 -2007 Pearson Education, Inc. All rights reserved.
76 Class Commission. Employee (cont) // set commission rate public void set. Commission. Rate( double rate ) { commission. Rate = ( rate > 0. 0 && rate < 1. 0 ) ? rate : 0. 0; } // end method set. Commission. Rate // return commission rate public double get. Commission. Rate() { return commission. Rate; } // end method get. Commission. Rate // calculate earnings public double earnings() { return commission. Rate * gross. Sales; } // end method earnings 1992 -2007 Pearson Education, Inc. All rights reserved.
77 Class Commission. Employee (cont) // return String representation of Commission. Employee 2 object public String to. String() { return String. format( "%s: %sn%s: %. 2 f", "commission employee", first. Name, "gross sales", gross. Sales, "commission rate", commission. Rate ); } // end method to. String } // end class Commission. Employee 2 1992 -2007 Pearson Education, Inc. All rights reserved.
78 Class Base. Plus. Commission. Employee public class Base. Plus. Commission. Employee extends Commission. Employee { private double base. Salary; // base salary per week // constructor public Base. Plus. Commission. Employee( String first, double sales, double rate, double salary ) { super( first, sales, rate ); set. Base. Salary( salary ); // validate and store base salary } // end six-argument Base. Plus. Commission. Employee 3 constructor 1992 -2007 Pearson Education, Inc. All rights reserved.
79 Class Base. Plus. Commission. Employee constructor // set base salary public void set. Base. Salary( double salary ) { base. Salary = ( salary < 0. 0 ) ? 0. 0 : salary; } // end method set. Base. Salary // return base salary public double get. Base. Salary() { return base. Salary; } // end method get. Base. Salary 1992 -2007 Pearson Education, Inc. All rights reserved.
Class Base. Plus. Commission. Employee (cont) 80 // calculate earnings public double earnings() { return base. Salary + super. earnings(); } // end method earnings // return String representation of Base. Plus. Commission. Employee 3 public String to. String() { return String. format( "%s %sn%s: %. 2 f", "base-salaried“, super. to. String(), "base salary", base. Salary); } // end method to. String } // end class Base. Plus. Commission. Employee 3 1992 -2007 Pearson Education, Inc. All rights reserved.
81 Note That The earning method of Base. Plus. Commission. Employee 3 There is still some repeated code Part of the code is the same as Commission. Employee 2’s code Similarly: to. String method most of the code of the to. String method of Commission. Employee 2 is repeated 1992 -2007 Pearson Education, Inc. All rights reserved.
82 Test class - Test public class Test { public static void main( String args[] ) // instantiate Commission. Employee object Commission. Employee Com. Emp = new Commission. Employee( "Sue", 10000, . 06 ); // instantiate Base. Plus. Commission. Employee 3 object Base. Plus. Commission. Employee base. Com. Emp = new Base. Plus. Commission. Employee( "Bob", 5000, . 04, 300 ); 1992 -2007 Pearson Education, Inc. All rights reserved.
83 Test class - Test 3 (cont) com. Emp. set. Gross. Sales( 500 ); // set gross sales com. Emp. set. Commission. Rate(. 1 ); // set commission rate System. out. printf( "n%s: nn%sn", "Updated employee information obtained by to. String", com. Emp ); Base. Com. Emp. set. Base. Salary( 1000 ); // set base salary System. out. printf( "n%s: nn%sn", "Updated employee information obtained by to. String", base. Com. Emp. to. String() ); } // end main } // end class Test 1992 -2007 Pearson Education, Inc. All rights reserved.
84 output Updated employee information obtained by to. String: commission employee: Sue gross sales: 500. 00 commission rate: 0. 10 Updated employee information obtained by to. String: base-salaried commission employee: Bob gross sales: 5000. 00 commission rate: 0. 04 base salary: 1000. 00 1992 -2007 Pearson Education, Inc. All rights reserved.
4. 4. 4 Commission. Employee. Base. Plus. Commission. Employee Inheritance Hierarchy Using protected Instance Variables (Cont. ) 85 • Using protected instance variables – Advantages • subclasses can modify values directly • Slight increase in performance – Avoid set/get method call overhead – Disadvantages • No validity checking – subclass can assign illegal value • Implementation dependent – subclass methods more likely dependent on superclass implementation – superclass implementation changes may result in subclass modifications • Fragile (brittle) software 1992 -2007 Pearson Education, Inc. All rights reserved.
86 Software Engineering Observation 4. 6 Use the protected access modifier when a superclass should provide a method only to its subclasses and other classes in the same package, but not to other clients. 1992 -2007 Pearson Education, Inc. All rights reserved.
87 Software Engineering Observation 4. 7 Declaring superclass instance variables private (as opposed to protected) enables the superclass implementation of these instance variables to change without affecting subclass implementations. 1992 -2007 Pearson Education, Inc. All rights reserved.
88 Error-Prevention Tip 4. 1 When possible, do not include protected instance variables in a superclass. Instead, include nonprivate methods that access private instance variables. This will ensure that objects of the class maintain consistent states. 1992 -2007 Pearson Education, Inc. All rights reserved.
9. 4. 5 Commission. Employee. Base. Plus. Commission. Employee Inheritance Hierarchy Uing private Instance Variables 89 • Reexamine hierarchy – Use the best software engineering practice • Declare instance variables as private • Provide public get and set methods • Use get method to obtain values of instance variables 1992 -2007 Pearson Education, Inc. All rights reserved.
Outline Declare private instance variables 90 Commission Employee 3. java (1 of 4) Lines 6 -10 1992 -2007 Pearson Education, Inc. All rights reserved.
Outline 91 Commission Employee 3. java (2 of 4) 1992 -2007 Pearson Education, Inc. All rights reserved.
Outline 92 Commission Employee 3. java (3 of 4) 1992 -2007 Pearson Education, Inc. All rights reserved.
Outline 93 Use get methods to obtain the Commission values of instance variables Employee 3. java (4 of 4) Line 87 Lines 94 -97 1992 -2007 Pearson Education, Inc. All rights reserved.
Outline 94 Base. Plus. Commission Employee 4. java Inherits from Commission. Employee 3 (1 of 2) 1992 -2007 Pearson Education, Inc. All rights reserved.
Outline 95 Base. Plus. Commission Invoke an overridden superclass method from a. Employee 4. java subclass (2 of 2) Line 33 &the 40 Use get methods to obtain values of instance variables Line 33 Lines 40 Invoke an overridden superclass method from a subclass 1992 -2007 Pearson Education, Inc. All rights reserved.
96 Common Programming Error 9. 3 When a superclass method is overridden in a subclass, the subclass version often calls the superclass version to do a portion of the work. Failure to prefix the superclass method name with the keyword super and a dot (. ) separator when referencing the superclass’s method causes the subclass method to call itself, creating an error called infinite recursion. Recursion, used correctly, is a powerful capability discussed in Chapter 15, Recursion. 1992 -2007 Pearson Education, Inc. All rights reserved.
Outline 97 Base. Plus. Commission Employee. Test 4. java Base. Plus. Commission. Employee 4 Create object. (1 of 2) Lines 9 -11 Lines 16 -25 Use inherited get methods to access inherited private instance variables Use Base. Plus. Commission. Employee 4 get method to access private instance variable. 1992 -2007 Pearson Education, Inc. All rights reserved.
Outline 98 Use Base. Plus. Commission. Employee 4 set method to modify private instance variable base. Salary. Base. Plus. Commission Employee. Test 4. java (2 of 2) 1992 -2007 Pearson Education, Inc. All rights reserved.
99 Class Commission. Employee 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 public class Commission. Employee 3 { private String first. Name; private String last. Name; private String social. Security. Number; private double gross. Sales; // gross weekly sales private double commission. Rate; // commission percentage // five-argument constructor public Commission. Employee 3( String first, String last, String ssn, double sales, double rate ) { // implicit call to Object constructor occurs here first. Name = first; last. Name = last; social. Security. Number = ssn; set. Gross. Sales( sales ); // validate and store gross sales set. Commission. Rate( rate ); // validate and store commission rate } // end five-argument Commission. Employee 3 constructor 1992 -2007 Pearson Education, Inc. All rights reserved.
100 Class Commission. Employee 3 (cont) 24 25 26 27 28 // set first name public void set. First. Name( String first ) { first. Name = first; } // end method set. First. Name 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 public String get. First. Name() { return first. Name; } // end method get. First. Name // return first name // set last name public void set. Last. Name( String last ) { last. Name = last; } // end method set. Last. Name // return last name public String get. Last. Name() { return last. Name; } // end method get. Last. Name 1992 -2007 Pearson Education, Inc. All rights reserved.
101 Class Commission. Employee 3 (cont) 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 // set social security number public void set. Social. Security. Number( String ssn ) { social. Security. Number = ssn; // should validate } // end method set. Social. Security. Number // return social security number public String get. Social. Security. Number() { return social. Security. Number; } // end method get. Social. Security. Number // set gross sales amount public void set. Gross. Sales( double sales ) { gross. Sales = ( sales < 0. 0 ) ? 0. 0 : sales; } // end method set. Gross. Sales // return gross sales amount public double get. Gross. Sales() { return gross. Sales; } // end method get. Gross. Sales 1992 -2007 Pearson Education, Inc. All rights reserved.
102 Class Commission. Employee 3 (cont) 73 public void set. Commission. Rate( double rate ) 74 { 75 commission. Rate = ( rate > 0. 0 && rate < 1. 0 ) ? rate : 0. 0; 76 } // end method set. Commission. Rate 77 78 // return commission rate 79 public double get. Commission. Rate() 80 { 81 return commission. Rate; 82 } // end method get. Commission. Rate 84 // calculate earnings 85 public double earnings() 86 { 87 return get. Commission. Rate() * get. Gross. Sales(); 88 } // end method earnings 1992 -2007 Pearson Education, Inc. All rights reserved.
103 Class Commission. Employee 3 (cont) 90 91 92 93 // return String representation of Commission. Employee 3 object public String to. String() { return String. format( "%s: %s %sn%s: %. 2 fn%s: %. 2 f", 94 "commission employee", get. First. Name(), get. Last. Name(), 95 "social security number", get. Social. Security. Number(), 96 "gross sales", get. Gross. Sales(), 97 "commission rate", get. Commission. Rate() ); 98 } // end method to. String 99 } // end class Commission. Employee 3 1992 -2007 Pearson Education, Inc. All rights reserved.
Class Base. Plus. Commission. Employee 4 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 104 public class Base. Plus. Commission. Employee 4 extends Commission. Employee 3 { private double base. Salary; // base salary per week // six-argument constructor public Base. Plus. Commission. Employee 4( String first, String last, String ssn, double sales, double rate, double salary ) { super( first, last, ssn, sales, rate ); set. Base. Salary( salary ); // validate and store base salary } // end six-argument Base. Plus. Commission. Employee 4 constructor // set base salary public void set. Base. Salary( double salary ) { base. Salary = ( salary < 0. 0 ) ? 0. 0 : salary; } // end method set. Base. Salary 1992 -2007 Pearson Education, Inc. All rights reserved.
Class Base. Plus. Commission. Employee 4 (cont) 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 105 // return base salary public double get. Base. Salary() { return base. Salary; } // end method get. Base. Salary // calculate earnings public double earnings() { return get. Base. Salary() + super. earnings(); } // end method earnings // return String representation of Base. Plus. Commission. Employee 4 public String to. String() { return String. format( "%s %sn%s: %. 2 f", "base-salaried", super. to. String(), "base salary", get. Base. Salary() ); } // end method to. String } // end class Base. Plus. Commission. Employee 4 1992 -2007 Pearson Education, Inc. All rights reserved.
106 Note That • The earnings method of Base. Plus. Commission. Employee 4 calls the earnings method of Commission. Employee 3 • Othewise part of the code is repeted • Similarly • to. String method of • Base. Plus. Commission. Employee 4 calls the to. String method of Commission. Employee 3 1992 -2007 Pearson Education, Inc. All rights reserved.
Test Class - Base. Plus. Commission. Employee. Test 4 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 107 public class Base. Plus. Commission. Employee. Test 4 { public static void main( String args[] ) { // instantiate Base. Plus. Commission. Employee 4 object Base. Plus. Commission. Employee 4 employee = new Base. Plus. Commission. Employee 4( "Bob", "Lewis", "333 -33 -3333", 5000, . 04, 300 ); // get base-salaried commission employee data System. out. println( "Employee information obtained by get methods: n" ); System. out. printf( "%s %sn", "First name is", employee. get. First. Name() ); System. out. printf( "%s %sn", "Last name is", employee. get. Last. Name() ); System. out. printf( "%s %sn", "Social security number is", employee. get. Social. Security. Number() ); System. out. printf( "%s %. 2 fn", "Gross sales is", employee. get. Gross. Sales() ); 1992 -2007 Pearson Education, Inc. All rights reserved.
Test Class - Base. Plus. Commission. Employee. Test 4 (cont) 108 24 System. out. printf( "%s %. 2 fn", "Commission rate is", 25 employee. get. Commission. Rate() ); 26 System. out. printf( "%s %. 2 fn", "Base salary is", 27 employee. get. Base. Salary() ); 28 29 employee. set. Base. Salary( 1000 ); // set base salary 30 31 System. out. printf( "n%s: nn%sn", 32 "Updated employee information obtained by to. String", 33 employee. to. String() ); 34 } // end main 35 } // end class Base. Plus. Commission. Employee. Test 4 1992 -2007 Pearson Education, Inc. All rights reserved.
Output - Base. Plus. Commission. Employee. Test 4 109 Employee information obtained by get methods: First name is Bob Last name is Lewis Social security number is 333 -33 -3333 Gross sales is 5000. 00 Commission rate is 0. 04 Base salary is 300. 00 Updated employee information obtained by to. String: base-salaried commission employee: Bob Lewis social security number: 333 -33 -3333 gross sales: 5000. 00 commission rate: 0. 04 base salary: 1000. 00 1992 -2007 Pearson Education, Inc. All rights reserved.
110 4. 5 Constructors in Subclasses • Instantiating subclass object – Chain of constructor calls • subclass constructor invokes superclass constructor – Implicitly or explicitly • Base of inheritance hierarchy – Last constructor called in chain is Object’s constructor – Original subclass constructor’s body finishes executing last – Example: Commission. Employee 3 Base. Plus. Commission. Employee 4 hierarchy • Commission. Employee 3 constructor called second last (last is Object constructor) • Commission. Employee 3 constructor’s body finishes execution second (first is Object constructor’s body) 1992 -2007 Pearson Education, Inc. All rights reserved.
111 Software Engineering Observation 4. 8 When a program creates a subclass object, the subclass constructor immediately calls the superclass constructor (explicitly, via super, or implicitly). The superclass constructor’s body executes to initialize the superclass’s instance variables that are part of the subclass object, then the subclass constructor’s body executes to initialize the subclass-only instance variables. (cont…) 1992 -2007 Pearson Education, Inc. All rights reserved.
112 Software Engineering Observation 9. 8 Java ensures that even if a constructor does not assign a value to an instance variable, the variable is still initialized to its default value (e. g. , 0 for primitive numeric types, false for booleans, null for references). 1992 -2007 Pearson Education, Inc. All rights reserved.
113 Class Commission. Employee public class Commission. Employee { protected String first. Name; protected double gross. Sales; // gross weekly sales protected double commission. Rate; // commission percentage // five-argument constructor public Commission. Employee( String first, double sales, double rate ) { // implicit call to Object constructor occurs here first. Name = first; set. Gross. Sales( sales ); // validate and store gross sales set. Commission. Rate( rate ); // validate and store commission rate System. out. printf( "n. Commission. Employee constructor: n%sn", this ); // optionally this. to. String or just to. String } // end Commission. Employee constructor 1992 -2007 Pearson Education, Inc. All rights reserved.
114 Class Commission. Employee (cont) // set first name public void set. First. Name( String first ) { first. Name = first; } // end method set. First. Name 30 // return first name public String get. First. Name() { return first. Name; } // end method get. First. Name 1992 -2007 Pearson Education, Inc. All rights reserved.
115 Class Commission. Employee (cont) // set gross sales amount public void set. Gross. Sales( double sales ) { gross. Sales = ( sales < 0. 0 ) ? 0. 0 : sales; } // end method set. Gross. Sales // return gross sales amount public double get. Gross. Sales() { return gross. Sales; } // end method get. Gross. Sales 1992 -2007 Pearson Education, Inc. All rights reserved.
116 Class Commission. Employee (cont) // set commission rate public void set. Commission. Rate( double rate ) { commission. Rate = ( rate > 0. 0 && rate < 1. 0 ) ? rate : 0. 0; } // end method set. Commission. Rate // return commission rate public double get. Commission. Rate() { return commission. Rate; } // end method get. Commission. Rate // calculate earnings public double earnings() { return commission. Rate * gross. Sales; } // end method earnings 1992 -2007 Pearson Education, Inc. All rights reserved.
117 Class Commission. Employee (cont) // return String representation of Commission. Employee 2 object public String to. String() { return String. format( "%s: %sn%s: %. 2 f", "commission employee", first. Name, "gross sales", gross. Sales, "commission rate", commission. Rate ); } // end method to. String } // end class Commission. Employee 2 1992 -2007 Pearson Education, Inc. All rights reserved.
118 Class Base. Plus. Commission. Employee public class Base. Plus. Commission. Employee extends Commission. Employee { private double base. Salary; // base salary per week // constructor public Base. Plus. Commission. Employee( String first, double sales, double rate, double salary ) { super( first, sales, rate ); set. Base. Salary( salary ); // validate and store base salary ); System. out. printf( "n. Base. Plus. Commission. Employee constructor: n%sn", this } // end six-argument Base. Plus. Commission. Employee 3 constructor 1992 -2007 Pearson Education, Inc. All rights reserved.
Class Base. Plus. Commission. Employee (cont) 119 constructor // set base salary public void set. Base. Salary( double salary ) { base. Salary = ( salary < 0. 0 ) ? 0. 0 : salary; } // end method set. Base. Salary // return base salary public double get. Base. Salary() { return base. Salary; } // end method get. Base. Salary 1992 -2007 Pearson Education, Inc. All rights reserved.
Class Base. Plus. Commission. Employee (cont) 120 // calculate earnings public double earnings() { return base. Salary + super. earnings(); } // end method earnings // return String representation of Base. Plus. Commission. Employee 3 public String to. String() { return String. format( "%s %sn%s: %. 2 f", "base-salaried“, super. to. String(), "base salary", base. Salary); } // end method to. String } // end class Base. Plus. Commission. Employee 3 1992 -2007 Pearson Education, Inc. All rights reserved.
121 Class Constructor. Test public class Constructor. Test { public static void main( String args[] ) { Commission. Employee employee 1 = new Commission. Employee( "Bob", 5000, . 04 ); System. out. println(); Base. Plus. Commission. Employee employee 2 = new Base. Plus. Commission. Employee( "Lisa", 2000, . 06, 800 ); System. out. println(); Base. Plus. Commission. Employee employee 3 = new Base. Plus. Commission. Employee( "Mark", 8000, . 15, 2000 ); } // end main } // end class Constructor. Test 1992 -2007 Pearson Education, Inc. All rights reserved.
122 output Commission. Employee constructor: commission employee: Bob gross sales: 5000. 00 commission rate: 0. 04 Commission. Employee constructor: base-salaried commission employee: Lisa gross sales: 2000. 00 commission rate: 0. 06 base salary: 0. 00 Base. Plus. Commission. Employee constructor: base-salaried commission employee: Lisa gross sales: 2000. 00 commission rate: 0. 06 base salary: 800. 00 1992 -2007 Pearson Education, Inc. All rights reserved.
123 output Commission. Employee constructor: base-salaried commission employee: Mark gross sales: 8000. 00 commission rate: 0. 15 base salary: 0. 00 Base. Plus. Commission. Employee constructor: base-salaried commission employee: Mark gross sales: 8000. 00 commission rate: 0. 15 base salary: 2000. 00 1992 -2007 Pearson Education, Inc. All rights reserved.
124 Outline • Commissi on. Employe e 4. java Constructor outputs message to demonstrate method call order. • (1 of 4) • Lines 2324 1992 -2007 Pearson Education, Inc. All rights reserved.
125 Outline • Commissi on. Employe e 4. java • (2 of 4) 1992 -2007 Pearson Education, Inc. All rights reserved.
126 Outline • Commissi on. Employe e 4. java • (3 of 4) 1992 -2007 Pearson Education, Inc. All rights reserved.
127 Outline • Commissi on. Employe e 4. java • (4 of 4) 1992 -2007 Pearson Education, Inc. All rights reserved.
128 Outline Constructor outputs message to demonstrate method call order. • Base. Plus. C ommission Employee 5. java • (1 of 2) • Lines 1516 1992 -2007 Pearson Education, Inc. All rights reserved.
129 Outline • Base. Plus. C ommission Employee 5. java • (2 of 2) 1992 -2007 Pearson Education, Inc. All rights reserved.
130 Outline Instantiate Commission. Employee 4 object • Construct or. Test • . java Instantiate two Base. Plus. Commission. Employee 5 objects to demonstrate order of subclass and superclass constructor method calls. • (1 of 2) • Lines 8 -9 • Lines 1219 1992 -2007 Pearson Education, Inc. All rights reserved.
131 Outline • Construct or. Test • . java Subclass Base. Plus. Commission. Employee 5 constructor body executes after superclass Commission. Employee 4’s constructor finishes execution. • (2 of 2) 1992 -2007 Pearson Education, Inc. All rights reserved.
132 Note That The earning method of Base. Plus. Commission. Employee 3 There is still some repeated code Part of the code is the same as Commission. Employee 2’s code Similarly: to. String method most of the code of the to. String method of Commission. Employee 2 is repeated 1992 -2007 Pearson Education, Inc. All rights reserved.
133 Class Commission. Employee 4 (cont) 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 // set first name public void set. First. Name( String first ) { first. Name = first; } // end method set. First. Name // return first name public String get. First. Name() { return first. Name; } // end method get. First. Name // set last name public void set. Last. Name( String last ) { last. Name = last; } // end method set. Last. Name // return last name public String get. Last. Name() { return last. Name; } // end method get. Last. Name 1992 -2007 Pearson Education, Inc. All rights reserved.
134 Class Commission. Employee 4 (cont) 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 // set social security number public void set. Social. Security. Number( String ssn ) { social. Security. Number = ssn; // should validate } // end method set. Social. Security. Number // return social security number public String get. Social. Security. Number() { return social. Security. Number; } // end method get. Social. Security. Number // set gross sales amount public void set. Gross. Sales( double sales ) { gross. Sales = ( sales < 0. 0 ) ? 0. 0 : sales; } // end method set. Gross. Sales // return gross sales amount public double get. Gross. Sales() { return gross. Sales; } // end method get. Gross. Sales 1992 -2007 Pearson Education, Inc. All rights reserved.
135 Class Commission. Employee 4 (cont) 75 76 77 78 79 74 81 82 83 84 85 // set commission rate public void set. Commission. Rate( double rate ) { commission. Rate = ( rate > 0. 0 && rate < 1. 0 ) ? rate : 0. 0; } // end method set. Commission. Rate // return commission rate public double get. Commission. Rate() { return commission. Rate; } // end method get. Commission. Rate 1992 -2007 Pearson Education, Inc. All rights reserved.
136 Class Commission. Employee 4 (cont) 87 // calculate earnings 88 public double earnings() 89 { 90 return get. Commission. Rate() * get. Gross. Sales(); 91 } // end method earnings 93// return String representation of Commission. Employee 4 object 94 public String to. String() 95 { 96 return String. format( "%s: %s %sn%s: %. 2 fn%s: %. 97 "commission employee", get. First. Name(), get. Last. Name(), 98 99 100 101 102 "social security number", get. Social. Security. Number(), "gross sales", get. Gross. Sales(), "commission rate", get. Commission. Rate() ); } // end method to. String } // end class Commission. Employee 4 1992 -2007 Pearson Education, Inc. All rights reserved.
Class Base. Plus. Commission. Employee 5 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 137 public class Base. Plus. Commission. Employee 5 extends Commission. Employee 4 { private double base. Salary; // base salary per week // six-argument constructor public Base. Plus. Commission. Employee 5( String first, String last, String ssn, double sales, double rate, double salary ) { super( first, last, ssn, sales, rate ); set. Base. Salary( salary ); // validate and store base salary System. out. printf( "n. Base. Plus. Commission. Employee 5 constructor: n%sn" , this ); } // end six-argument Base. Plus. Commission. Employee 5 constructor // set base salary public void set. Base. Salary( double salary ) { base. Salary = ( salary < 0. 0 ) ? 0. 0 : salary; } // end method set. Base. Salary 1992 -2007 Pearson Education, Inc. All rights reserved.
Class Base. Plus. Commission. Employee 5 (cont) 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 138 // return base salary public double get. Base. Salary() { return base. Salary; } // end method get. Base. Salary // calculate earnings public double earnings() { return get. Base. Salary() + super. earnings(); } // end method earnings // return String representation of Base. Plus. Commission. Employee 5 public String to. String() { return String. format( "%s %sn%s: %. 2 f", "base-salaried", super. to. String(), "base salary", get. Base. Salary() ); } // end method to. String } // end class Base. Plus. Commission. Employee 5 1992 -2007 Pearson Education, Inc. All rights reserved.
139 4. 6 Software Engineering with Inheritance • Customizing existing software – Inherit from existing classes • Include additional members • Redefine superclass members • No direct access to superclass’s source code – Link to object code – Independent software vendors (ISVs) • Develop proprietary code for sale/license – Available in object-code format • Users derive new classes – Without accessing ISV proprietary source code 1992 -2007 Pearson Education, Inc. All rights reserved.
140 Software Engineering Observation 4. 9 Despite the fact that 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. Developers in industry want to ensure that they are extending a solid class—for example, a class that performs well and is implemented securely. 1992 -2007 Pearson Education, Inc. All rights reserved.
141 Software Engineering Observation 4. 10 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 instance variables and methods and place them in a superclass. Then the designer should use inheritance to develop subclasses, specializing them with capabilities beyond those inherited from the superclass. 1992 -2007 Pearson Education, Inc. All rights reserved.
142 Software Engineering Observation 4. 11 Declaring a subclass does not affect its superclass’s source code. Inheritance preserves the integrity of the superclass. 1992 -2007 Pearson Education, Inc. All rights reserved.
143 Software Engineering Observation 4. 12 Just as designers of non-object-oriented systems should avoid method proliferation, 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. The alternative is to create fewer classes that provide more substantial functionality, but such classes might prove cumbersome. 1992 -2007 Pearson Education, Inc. All rights reserved.
144 Performance Tip 4. 1 If subclasses are larger than they need to be (i. e. , contain too much functionality), memory and processing resources might be wasted. Extend the superclass that contains the functionality that is closest to what is needed. 1992 -2007 Pearson Education, Inc. All rights reserved.
145 4. 7 Object Class • Class Object methods – – – – clone equals finalize get. Class hash. Code notify, notify. All, wait to. String 1992 -2007 Pearson Education, Inc. All rights reserved.
146 Fig. 9. 18 | Object methods that are inherited directly or indirectly by all classes. (Part 1 of 4) 1992 -2007 Pearson Education, Inc. All rights reserved.
147 Fig. 9. 18 | Object methods that are inherited directly or indirectly by all classes. (Part 2 of 4) 1992 -2007 Pearson Education, Inc. All rights reserved.
148 Fig. 9. 18 | Object methods that are inherited directly or indirectly by all classes. (Part 3 of 4) 1992 -2007 Pearson Education, Inc. All rights reserved.
149 Fig. 9. 18 | Object methods that are inherited directly or indirectly by all classes. (Part 4 of 4) 1992 -2007 Pearson Education, Inc. All rights reserved.
150 Example – University Members • Person Students Grad. Student • Person Staff Faculty 1992 -2007 Pearson Education, Inc. All rights reserved.
151 The Person Class • Person – Attrubutes: • name • Others: last. Name, SSN, birth. Date, … – Behavior • to. String, other display methods • Other get and set methods 1992 -2007 Pearson Education, Inc. All rights reserved.
152 The Student Class • Student – Attrubutes: specific to each student • gpa • Others: student ID, courses taken, , … – Attributes: claswide • Repeating limit: applied to all students – Behavior • • • to. String, other display methods status: normal or repeating if gpa < 2. 0 then “repeating” else “normal” Other get and set methods 1992 -2007 Pearson Education, Inc. All rights reserved.
153 The Graduagte. Student Class • Graduate. Student – Attrubutes: • Thesis title • Others: graduated university – Attributes: claswide Onprobesion limit: applied to all graduate students – Behavior • • • to. String, other display methods status: normal or onprobesion if gpa < 2. 5 then “onprobesion” else “normal” Other get and set methods 1992 -2007 Pearson Education, Inc. All rights reserved.
154 The Person Class implementation public class Person { protected String name; public Person(String name) { this. name = name; } // end of constructor Person public String to. String() { return String. format(“%10 s: %20 sn”, “Name”, name); } // end of method } // end of class 1992 -2007 Pearson Education, Inc. All rights reserved.
155 The Student Class implementation public class Student extends Person { protected double gpa; private static final double REP_LIMIT = 2. 0; public Student(String name, double gpa) { super(name); set. Gpa(gpa); } // end of constructor Student public void set. Gpa(double gpa ) { this. gpa = (gpa >=0. 0 && gpa <= 4. 0) ? gpa : 0. 0; } // end of methd 1992 -2007 Pearson Education, Inc. All rights reserved.
156 The Student Class implementation private String get. Status() { return ( gpa >= REP_LIMIT) ? “normal” ; “repeating”; } // end of method public String to. String() { return String. format(“%s%10 s: %20 sn”, super. to. String(), “Status”, get. Status()); } // end of method } // end of class 1992 -2007 Pearson Education, Inc. All rights reserved.
157 The Grad. Student Class implementation public class Grad. Student extends Student { private String thesis; private static final double ONPR_LIMIT = 2. 5; public Graduate. Student( String name , double gpa , String thesis ) { super(name, gpa); this. thesis = thesis; } // end of constructor Grad. Student 1992 -2007 Pearson Education, Inc. All rights reserved.
158 The Grad. Student Class implementation private String get. Status() { return ( gpa >= ONPR_LIMIT) ? “normal” ; “onprobesion”; } // end of method , public String to. String() { return String. format(“%s%10 s: %20 sn”, super. to. String(), “Thesis”, thesis); } // end of method } // end of class 1992 -2007 Pearson Education, Inc. All rights reserved.
159 Test Class Inplementation public class Test { public static void main(String args[]) { // create a Student and a grad. Student object Student ali = new Student(“ali”, 2. 8); Grad. Student ahmet = new Grad. Student(“ahmet”, 2. 3, ”MIS in Turkey”); // display students System. out. printf(“%sn%s”, ali, ahmet. to. String()); } // end of main } // end of Test class 1992 -2007 Pearson Education, Inc. All rights reserved.
160 Output Name: ali Status: Name: normal ahmet Status: Thesis: onprob MIS in Turkey 1992 -2007 Pearson Education, Inc. All rights reserved.
161 Explanation • ali’s to. String method is invoked which calls the to. String method of Person to display name and then displays status of the Student object as normal • ahmet’s to. String method is invoked which calls the to. String method of Student After a call to super’s to. String to display name, the to. String method calls the status method. 1992 -2007 Pearson Education, Inc. All rights reserved.
162 Explanation (cont. ) • Which status method is called? – – The Student’s or Craduate. Studnet’s • Although it is invoked in the pease of code of Student class’s to. String, type of the object determines which method wil be invoked. • Since the object refered by ahmet is of type Graduate. Student, no mather at what point in the class hierarchy it is invoked always the Graduate. Student’s status method is executed. 1992 -2007 Pearson Education, Inc. All rights reserved.
163 Example – Bank Classes • In a bank different types of bank accounts • inherited from accout class • Account • Saving account, 1992 -2007 Pearson Education, Inc. All rights reserved.
164 Example – Bank Classes • Account class has – İnstance variables • Name: String • Balance: double • Code: String – Behavior: • Deposit • Withdraw • get. Balance • get. Acc. Code 1992 -2007 Pearson Education, Inc. All rights reserved.
165 Example – Bank Classes • Saving account pays interest rate to the owners • Additional instance variables – Interest rate: static double • Static because not depends on specific accouts – term: int final • 1 month, 3 month … • Non-static depends on the account • Final as an account is opended determinde its value not change anymore for each specific accout – Behavior: • Calculate interet on deposits • Overrides deposit, withdraw 1992 -2007 Pearson Education, Inc. All rights reserved.
166 Example – Bank Classes • A simple total interest rate calculation would be public double calculate. Interest() { return balance*(1+interest. Rate); } Here interst. Rate is a static variable of Saving. Account class Balanc is a instance variable of the Account class inherited by Saving. Account 1992 -2007 Pearson Education, Inc. All rights reserved.
167 Example – Bank Classes • In reality interest accumulated depends on the term of the account • From the last apperation the customer made – Deposit or withdraw money – Calculate haw many months has passed – Caluate actif months as the whole number of months since the last update of the account 1992 -2007 Pearson Education, Inc. All rights reserved.
168 Example – Bank Classes • Example: • Account is last updated in month 5 • Current month is 13 • Term of the account is 3 months • 13 – 5 = 8 • 8 months has passed • 8 div 3 = 2 • 2 whole three month period • For the remaining two months no interest rate accumulation • Actif montsh is 8/3 = 2 • Or equivalently • Actif montsh is 5 – (8 % 3) = 2 • Total balance = balance*(1+int. Rate)2 1992 -2007 Pearson Education, Inc. All rights reserved.
169 Example – Bank Classes • Whenever an oppoertatin is made to an Saving. Accout first calculate the total new balnace • Whole. Terms = (current. Monthmonth. Last. Update)/term; – balance = balance*Math. pow(1+int. Rate, whole. Terms); 1992 -2007 Pearson Education, Inc. All rights reserved.
170 Example – Bank Classes 8 end of first term 5: lsat update 13 current month Last updeat at month 5 Current month 10 Term of Account is 3 two 3 month period İnterest rate charge only 3 months Any accumulation in 9 and 10 monts would be laost 1992 -2007 Pearson Education, Inc. All rights reserved.
171 • At the end of each opperation update the last. Updated. Month variable • Suppose customer withdraw money on month 10 • After the withdraw opperation make last. Updated. Month of the Saving. Account to the current. Month 1992 -2007 Pearson Education, Inc. All rights reserved.
- Slides: 171