Download presentation
Presentation is loading. Please wait.
1
Object-Oriented Programming: Inheritance
4 Object-Oriented Programming: Inheritance
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.
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 CommissionEmployee Class 9.4.2 Creating a BasePlusCommissionEmployee Class without Using Inheritance 9.4.3 Creating a CommissionEmployee– BasePlusCommissionEmployee Inheritance Hierarchy 9.4.4 CommissionEmployee– BasePlusCommissionEmployee Inheritance Hierarchy Using protected Instance Variables 9.4.5 CommissionEmployee– BasePlusCommissionEmployee Inheritance Hierarchy Using private Instance Variables
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
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
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
7
4.2 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
8
Fig. 9.1 | Inheritance examples.
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
10
Fig. 9.2 | Inheritance hierarchy for university CommunityMembers
11
Fig. 9.3 | Inheritance hierarchy for Shapes.
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 (.)
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.
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.
15
4.4 Relationship between Superclasses and Subclasses
Superclass and subclass relationship Example: CommissionEmployee/BasePlusCommissionEmployee inheritance hierarchy CommissionEmployee First name, last name, SSN, commission rate, gross sale amount BasePlusCommissionEmployee Base salary
16
4.4 Relationship between Superclasses and Subclasses
Superclass and subclass relationship Example: CommissionEmployee/BasePlusCommissionEmployee inheritance hierarchy CommissionEmployee First name, last name, SSN, commission rate, gross sale amount Get commision from sales Earnings = grossSales*commission rate BasePlusCommissionEmployee Base salary They have a base salary + get commissions from sales
17
4.4 Relationship between Superclasses and Subclasses
CommissionEmployee 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 toString method to return a string representation of the objcet A method to calculate the revenue
18
4.4.1 Creating and Using a CommissionEmployee Class
Class CommissionEmployee 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
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.
20
Outline Declare private instance variables
Class CommissionEmployee extends class Object CommissionEmployee .java (1 of 4) Line 4 Lines 6-10 Line 16 Lines 17-21 Lines 20-21 Implicit call to Object constructor Initialize instance variables Invoke methods setGrossSales and setCommissionRate to validate data
21
Outline CommissionEmployee .java (2 of 4)
22
Outline (3 of 4) Lines 85-88 Calculate earnings
CommissionEmployee .java (3 of 4) Lines 85-88 Calculate earnings
23
Outline Override method toString of class Object (4 of 4) Lines 91-98
CommissionEmployee .java (4 of 4) Lines 91-98
24
Class CommissionEmployee
4 public class CommissionEmployee extends Object 5 { 6 private String firstName; 7 private String lastName; 8 private String socialSecurityNumber; 9 private double grossSales; // gross weekly sales 10 private double commissionRate; // commission percentage 11 12 // five-argument constructor 13 public CommissionEmployee( String first, String last, String ssn, double sales, double rate ) 15 { // implicit call to Object constructor occurs here firstName = first; lastName = last; socialSecurityNumber = ssn; setGrossSales( sales ); // validate and store gross sales setCommissionRate( rate ); // validate and store commission rate 22 } // end five-argument CommissionEmployee constructor
25
Class CommissionEmployee (cont)
public class CommissionEmployee extends Object { private String firstName; private double grossSales; // gross weekly sales private double commissionRate; // commission percentage // five-argument constructor public CommissionEmployee( String first, String last, String ssn, double sales, double rate ) // implicit call to Object constructor occurs here firstName = first; lastName = last; socialSecurityNumber = ssn; setGrossSales( sales ); // validate and store gross sales setCommissionRate( rate ); // validate and store commission rate } // end five-argument CommissionEmployee constructor
26
Class CommissionEmployee (cont)
24 // set first name 25 public void setFirstName( String first ) 26 { firstName = first; 28 } // end method setFirstNa 30 // return first name 31 public String getFirstName() 32 { return firstName; 34 } // end method getFirstName 35 36 // set last name 37 public void setLastName( String last ) 38 { lastName = last; 40 } // end method setLastName 41 42 // return last name 43 public String getLastName() 44 { return lastName; 46 } // end method getLastName
27
Class CommissionEmployee (cont)
48 // set social security number 49 public void setSocialSecurityNumber( String ssn ) 50 { socialSecurityNumber = ssn; // should validate 52 } // end method setSocialSecurityNumber 53 54 // return social security number 55 public String getSocialSecurityNumber() 56 { return socialSecurityNumber; 58 } // end method getSocialSecurityNumber 60 // set gross sales amount 61 public void setGrossSales( double sales ) 62 { grossSales = ( sales < 0.0 ) ? 0.0 : sales; 64 } // end method setGrossSales 65 66 // return gross sales amount 67 public double getGrossSales() 68 { return grossSales; 70 } // end method getGrossSales 71
28
Class CommissionEmployee (cont)
78 // return commission rate 79 public double getCommissionRate() 80 { return commissionRate; 82 } // end method getCommissionRate 72 // set commission rate 73 public void setCommissionRate( double rate ) 74 { commissionRate = ( rate > 0.0 && rate < 1.0 ) ? rate : 0.0; 76 } // end method setCommissionRate 84 // calculate earnings 85 public double earnings() 86 { return commissionRate * grossSales; 88 } // end method earnings
29
Class CommissionEmployee (cont)
90 // return String representation of CommissionEmployee object 91 public String toString() 92 { return String.format( "%s: %s %s\n%s: %s\n%s: %.2f\n%s: %.2f", "commission employee", firstName, lastName, "social security number", socialSecurityNumber, "gross sales", grossSales, "commission rate", commissionRate ); 98 } // end method toString 99 } // end class CommissionEmployee
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…)
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.
32
Outline Instantiate CommissionEmployee object (1 of 2) Lines 9-10
CommissionEmployee Test.java (1 of 2) Lines 9-10 Lines 15-25 Line 26-27 Use CommissionEmployee’s get methods to retrieve the object’s instance variable values Use CommissionEmployee’s set methods to change the object’s instance variable values
33
Outline Implicitly call object’s toString method (2 of 2) Line 30
CommissionEmployee Test.java (2 of 2) Line 30 Program output
34
Test class - CommissionEmployeeTest
The test class creates an CommissionEmployee object and Print its characteristics using get methods Modify its grossSales using the setGrossSales method Print the objcet using the toString method
35
Test class - CommissionEmployeeTest
4 public class CommissionEmployeeTest 5 { 6 public static void main( String args[] ) 7 { // instantiate CommissionEmployee object CommissionEmployee employee = new CommissionEmployee( "Sue", "Jones", " ", 10000, .06 ); 11 // get commission employee data System.out.println( "Employee information obtained by get methods: \n" ); System.out.printf( "%s %s\n", "First name is", employee.getFirstName() ); System.out.printf( "%s %s\n", "Last name is", employee.getLastName() ); System.out.printf( "%s %s\n", "Social security number is", employee.getSocialSecurityNumber() ); System.out.printf( "%s %.2f\n", "Gross sales is", employee.getGrossSales() ); System.out.printf( "%s %.2f\n", "Commission rate is", employee.getCommissionRate() );
36
Test class - CommissionEmployeeTest (cont)
employee.setGrossSales( 500 ); // set gross sales employee.setCommissionRate( .1 ); // set commission rate 28 System.out.printf( "\n%s:\n\n%s\n", "Updated employee information obtained by toString", employee ); 31 } // end main 32 } // end class CommissionEmployeeTest
37
output-CommissionEmployeeTest
Employee information obtained by get methods: First name is Sue Last name is Jones Social security number is Gross sales is Commission rate is 0.06 Updated employee information obtained by toString: commission employee: Sue Jones social security number: gross sales: commission rate: 0.10
38
Class BasePlusCommissionEmployee
4.4.2 Creating a BasePlusCommissionEmployee Class without Using Inheritance Class BasePlusCommissionEmployee Implicitly extends Object Much of the code is similar to CommissionEmployee private instance variables public methods constructor Additions private instance variable baseSalary Methods setBaseSalary and getBaseSalary
39
Outline (1 of 4) Line 12 Add instance variable baseSalary Line 24
BasePlusCommission Employee.java (1 of 4) Line 12 Line 24 Add instance variable baseSalary Use method setBaseSalary to validate data
40
Outline BasePlusCommission Employee.java (2 of 4)
41
Outline BasePlusCommission Employee.java (3 of 4)
42
Outline Method setBaseSalary validates data and sets instance variable baseSalary BasePlusCommission Employee.java (4 of 4) Lines 88-91 Lines 94-97 Line 102 Lines Method getBaseSalary returns the value of instance variable baseSalary Update method earnings to calculate the earnings of a base-salaried commission employee Update method toString to display base salary
43
Outline Instantiate BasePlusCommissionEmployee object (1 of 2)
BasePlusCommission EmployeeTest.java (1 of 2) Line 9-11 Lines 16-27 Instantiate BasePlusCommissionEmployee object Use BasePluCommissionEmployee’s get methods to retrieve the object’s instance variable values
44
Outline Use BasePlusCommissionEmployee’s setBaseSalary methods to set base salary Explicitly call object’s toString method BasePlusCommission EmployeeTest.java (2 of 2) Line 29 Line 33 Program output
45
Class BasePlusCommissionEmployee
5 public class BasePlusCommissionEmployee 6 { 7 private String firstName; 8 private String lastName; 9 private String socialSecurityNumber; 10 private double grossSales; // gross weekly sales 11 private double commissionRate; // commission percentage 12 private double baseSalary; // base salary per week 13 14 // six-argument constructor 15 public BasePlusCommissionEmployee( String first, String last, String ssn, double sales, double rate, double salary ) 17 { // implicit call to Object constructor occurs here firstName = first; lastName = last; socialSecurityNumber = ssn; setGrossSales( sales ); // validate and store gross sales setCommissionRate( rate ); // validate and store commission rate setBaseSalary( salary ); // validate and store base salary 25 } // end six-argument BasePlusCommissionEmployee constructor
46
Class BasePlusCommissionEmployee (cont)
27 // set first name 28 public void setFirstName( String first ) 29 { firstName = first; 31 } // end method setFirstName 32 33 // return first name 34 public String getFirstName() 35 { return firstName; 37 } // end method getFirstName 38 39 // set last name 40 public void setLastName( String last ) 41 { lastName = last; 43 } // end method setLastName 44 45 // return last name 46 public String getLastName() 47 { return lastName; 49 } // end method getLastName
47
Class BasePlusCommissionEmployee (cont)
51 // set social security number 52 public void setSocialSecurityNumber( String ssn ) 53 { socialSecurityNumber = ssn; // should validate 55 } // end method setSocialSecurityNumber 57 // return social security number 58 public String getSocialSecurityNumber() 59 { return socialSecurityNumber; 61 } // end method getSocialSecurityNumber 62 63 // set gross sales amount 64 public void setGrossSales( double sales ) 65 { grossSales = ( sales < 0.0 ) ? 0.0 : sales; 67 } // end method setGrossSales 68 69 // return gross sales amount 70 public double getGrossSales() 71 { return grossSales; 73 } // end method getGrossSales
48
Class BasePlusCommissionEmployee (cont)
75 // set commission rate 76 public void setCommissionRate( double rate ) 77 { commissionRate = ( rate > 0.0 && rate < 1.0 ) ? rate : 0.0; 79 } // end method setCommissionRate 80 81 // return commission rate 82 public double getCommissionRate() 83 { return commissionRate; 85 } // end method getCommissionRate 87 // set base salary 88 public void setBaseSalary( double salary ) 89 { baseSalary = ( salary < 0.0 ) ? 0.0 : salary; 91 } // end method setBaseSalary 92 93 // return base salary 94 public double getBaseSalary() 95 { return baseSalary; 97 } // end method getBaseSalary 98 99 // calculate earnings public double earnings() { return baseSalary + ( commissionRate * grossSales ); } // end method earnings
49
Class BasePlusCommissionEmployee (cont)
// return String representation of BasePlusCommissionEmployee 106 public String toString() 107 { 108 return String.format( 109 "%s: %s %s\n%s: %s\n%s: %.2f\n%s: %.2f\n%s: %.2f", 110 "base-salaried commission employee", firstName, lastName, 111 "social security number", socialSecurityNumber, 112 "gross sales", grossSales, "commission rate", commissionRate, 113 "base salary", baseSalary ); 114 } // end method toString 115 } // end class BasePlusCommissionEmployee
50
Test class - BasePlusCommissionEmployeeTest
4 public class BasePlusCommissionEmployeeTest 5 { 6 public static void main( String args[] ) 7 { // instantiate BasePlusCommissionEmployee object BasePlusCommissionEmployee employee = new BasePlusCommissionEmployee( "Bob", "Lewis", " ", 5000, .04, 300 ); 12 // get base-salaried commission employee data System.out.println( "Employee information obtained by get methods: \n" ); System.out.printf( "%s %s\n", "First name is", employee.getFirstName() ); System.out.printf( "%s %s\n", "Last name is", employee.getLastName() ); System.out.printf( "%s %s\n", "Social security number is", employee.getSocialSecurityNumber() ); System.out.printf( "%s %.2f\n", "Gross sales is", employee.getGrossSales() ); System.out.printf( "%s %.2f\n", "Commission rate is", employee.getCommissionRate() ); System.out.printf( "%s %.2f\n", "Base salary is", employee.getBaseSalary() );
51
Test class - BasePlusCommissionEmployeeTest (cont)
employee.setBaseSalary( 1000 ); // set base salary 30 System.out.printf( "\n%s:\n\n%s\n", "Updated employee information obtained by toString", employee.toString() ); 34 } // end main 35 } // end class BasePlusCommissionEmployeeTest
52
output Employee information obtained by get methods: First name is Bob
Last name is Lewis Social security number is Gross sales is Commission rate is 0.04 Base salary is Updated employee information obtained by toString: base-salaried commission employee: Bob Lewis social security number: gross sales: commission rate: 0.04 base salary:
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 “copy-and-paste” approach, in situations where you want one class to “absorb” the instance variables and methods of another class.
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.
55
Class BasePlusCommissionEmployee2
4.4.3 Creating a CommissionEmployee-BasePlusCommiionEmployee Inheritance Hierarchy Class BasePlusCommissionEmployee2 Extends class CommissionEmployee Is a CommissionEmployee Has instance variable baseSalary Inherits public and protected members Constructor not inherited
56
Outline Class BasePluCommissionEmployee2 is a subclass of CommissionEmployee BasePlusCommission Employee2.java (1 of 3) Line 4 Line 13 Invoke the superclass constructor using the superclass constructor call syntax
57
Outline BasePlusCommission Employee2.java (2 of 3) Line 34 Lines 41-46 Compiler generates errors because superclass’s instance variable commissionRate and grossSales are private Compiler generates errors because superclass’s instance variable firstName, lastName, socialSecurityNumber, grossSales and commissionRate are private
58
Outline (3 of 3) Compiler generated errorss
BasePlusCommission Employee2.java (3 of 3) Compiler generated errorss
59
Class BasePlusCommissionEmployee2
4 public class BasePlusCommissionEmployee2 extends CommissionEmployee 5 { private double baseSalary; // base salary per week 7 8 // six-argument constructor 9 public BasePlusCommissionEmployee2( String first, String last, String ssn, double sales, double rate, double salary ) 11 { 12 // explicit call to superclass CommissionEmployee constructor 13 super( first, last, ssn, sales, rate ); 14 15 setBaseSalary( amount ); // validate and store base salary 16 } // end six-argument BasePlusCommissionEmployee2 constructor 17
60
Class BasePlusCommissionEmployee2 (cont)
18 // set base salary 19 public void setBaseSalary( double salary ) 20 { baseSalary = ( salary < 0.0 ) ? 0.0 : salary; 22 } // end method setBaseSalary 24 // return base salary 25 public double getBaseSalary() 26 { return baseSalary; 28 } // end method getBaseSalary 29 30 // calculate earnings 31 public double earnings() 32 { // not allowed: commissionRate and grossSales private in superclass return baseSalary + ( commissionRate * grossSales ); 35 } // end method earnings
61
Class BasePlusCommissionEmployee2 (cont)
37 // return String representation of BasePlusCommissionEmployee2 38 public String toString() 39 { // not allowed: attempts to access private superclass members return String.format( "%s: %s %s\n%s: %s\n%s: %.2f\n%s: %.2f\n%s: %.2f", "base-salaried commission employee", firstName, lastName, "social security number", socialSecurityNumber, "gross sales", grossSales, "commission rate", commissionRate, "base salary", baseSalary ); 47 } // end method toString 48 } // end class BasePlusCommissionEmployee2
62
putput BasePlusCommissionEmployee2.java:34: commissionRate has private access in CommissionEmployee return baseSalary + ( commissionRate * grossSales ); ^ BasePlusCommissionEmployee2.java:34: grossSales has private access in CommissionEmployee BasePlusCommissionEmployee2.java:43: firstName has private access in CommissionEmployee "base-salaried commission employee", firstName, lastName, BasePlusCommissionEmployee2.java:43: lastName has private access in CommissionEmployee BasePlusCommissionEmployee2.java:44: socialSecurityNumber has private access in CommissionEmployee "social security number", socialSecurityNumber, BasePlusCommissionEmployee2.java:45: grossSales has private access in CommissionEmployee "gross sales", grossSales, "commission rate", commissionRate, BasePlusCommissionEmployee2.java:45: commissionRate has private access in CommissionEmployee 7 errors
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.
64
Use protected instance variables
4.4.4 CommissionEmployee-BasePlusCommissionEmployee Inheritance Hierarchy Using protected Instance Variables Use protected instance variables Enable class BasePlusCommissionEmployee to directly access superclass instance variables Superclass’s protected members are inherited by all subclases of that superclass
65
Outline Declare protected instance variables (1 of 4) Line 6-10
Commission Employee2.java (1 of 4) Line 6-10
66
Outline Commission Employee2.java (2 of 4)
67
Outline Commission Employee2.java (3 of 4)
68
Outline Commission Employee2.java (4 of 4)
69
Outline (1 of 2) Line 13 Must call superclass’s constructor
BasePlusCommission Employee3.java (1 of 2) Line 13 Must call superclass’s constructor
70
Outline Directly access superclass’s protected instance variables BasePlusCommission Employee3.java (2 of 2) Line 32 Lines 38-43
71
Outline BasePlusCommission EmployeeTest3.java (1 of 2)
72
Outline BasePlusCommission EmployeeTest3.java (2 of 2) Program output
73
Class CommissionEmployee
public class CommissionEmployee extends Object // optional { protected String firstName; protected double grossSales; // gross weekly sales protected double commissionRate; // commission percentage // five-argument constructor public CommissionEmployee( String first, double sales, double rate ) // implicit call to Object constructor occurs here firstName = first; setGrossSales( sales ); // validate and store gross sales setCommissionRate( rate ); // validate and store commission rate } // end CommissionEmployee constructor
74
Class CommissionEmployee (cont)
// set first name public void setFirstName( String first ) { firstName = first; } // end method setFirstName // return first name public String getFirstName() return firstName; } // end method getFirstName
75
Class CommissionEmployee (cont)
// set gross sales amount public void setGrossSales( double sales ) { grossSales = ( sales < 0.0 ) ? 0.0 : sales; } // end method setGrossSales // return gross sales amount public double getGrossSales() return grossSales; } // end method getGrossSales
76
Class CommissionEmployee (cont)
// set commission rate public void setCommissionRate( double rate ) { commissionRate = ( rate > 0.0 && rate < 1.0 ) ? rate : 0.0; } // end method setCommissionRate // return commission rate public double getCommissionRate() return commissionRate; } // end method getCommissionRate // calculate earnings public double earnings() return commissionRate * grossSales; } // end method earnings
77
Class CommissionEmployee (cont)
// return String representation of CommissionEmployee2 object public String toString() { return String.format( "%s: %s\n%s: %.2f\n%s: %.2f", "commission employee", firstName, "gross sales", grossSales, "commission rate", commissionRate ); } // end method toString } // end class CommissionEmployee2
78
Class BasePlusCommissionEmployee
public class BasePlusCommissionEmployee extends CommissionEmployee { private double baseSalary; // base salary per week // constructor public BasePlusCommissionEmployee( String first, double sales, double rate, double salary ) super( first, sales, rate ); setBaseSalary( salary ); // validate and store base salary } // end six-argument BasePlusCommissionEmployee3 constructor
79
Class BasePlusCommissionEmployee
constructor // set base salary public void setBaseSalary( double salary ) { baseSalary = ( salary < 0.0 ) ? 0.0 : salary; } // end method setBaseSalary // return base salary public double getBaseSalary() return baseSalary; } // end method getBaseSalary
80
Class BasePlusCommissionEmployee (cont)
// calculate earnings public double earnings() { return baseSalary + super.earnings(); } // end method earnings // return String representation of BasePlusCommissionEmployee3 public String toString() return String.format( "%s %s\n%s: %.2f", "base-salaried“, super.toString(), "base salary", baseSalary); } // end method toString } // end class BasePlusCommissionEmployee3
81
Note That The earning method of BasePlusCommissionEmployee3
There is still some repeated code Part of the code is the same as CommissionEmployee2’s code Similarly: toString method most of the code of the toString method of CommissionEmployee2 is repeated
82
Test class - Test public class Test {
public static void main( String args[] ) // instantiate CommissionEmployee object CommissionEmployee ComEmp = new CommissionEmployee( "Sue", 10000, .06 ); // instantiate BasePlusCommissionEmployee3 object BasePlusCommissionEmployee baseComEmp = new BasePlusCommissionEmployee( "Bob", 5000, .04, 300 );
83
Test class - Test3 (cont)
comEmp.setGrossSales( 500 ); // set gross sales comEmp.setCommissionRate( .1 ); // set commission rate System.out.printf( "\n%s:\n\n%s\n", "Updated employee information obtained by toString", comEmp ); BaseComEmp.setBaseSalary( 1000 ); // set base salary System.out.printf( "\n%s:\n\n%s\n", "Updated employee information obtained by toString", baseComEmp.toString() ); } // end main } // end class Test
84
output Updated employee information obtained by toString:
commission employee: Sue gross sales: commission rate: 0.10 base-salaried commission employee: Bob gross sales: commission rate: 0.04 base salary:
85
Using protected instance variables
4.4.4 CommissionEmployee-BasePlusCommissionEmployee Inheritance Hierarchy Using protected Instance Variables (Cont.) 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
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.
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.
88
Error-Prevention Tip 4.1 When possible, do not include protected instance variables in a superclass. Instead, include non-private methods that access private instance variables. This will ensure that objects of the class maintain consistent states.
89
9.4.5 CommissionEmployee-BasePlusCommissionEmployee Inheritance Hierarchy Uing private Instance Variables 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
90
Outline Declare private instance variables (1 of 4) Lines 6-10
Commission Employee3.java (1 of 4) Lines 6-10
91
Outline Commission Employee3.java (2 of 4)
92
Outline Commission Employee3.java (3 of 4)
93
Outline Use get methods to obtain the values of instance variables
Commission Employee3.java (4 of 4) Line 87 Lines 94-97
94
Outline (1 of 2) Inherits from CommissionEmployee3
BasePlusCommission Employee4.java (1 of 2) Inherits from CommissionEmployee3
95
Outline Invoke an overridden superclass method from a subclass
BasePlusCommission Employee4.java (2 of 2) Line 33 & 40 Line 33 Lines 40 Use get methods to obtain the values of instance variables Invoke an overridden superclass method from a subclass
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.
97
Outline Create BasePlusCommissionEmployee4 object. (1 of 2) Lines 9-11
BasePlusCommission EmployeeTest4.java (1 of 2) Lines 9-11 Lines 16-25 Create BasePlusCommissionEmployee4 object. Use inherited get methods to access inherited private instance variables Use BasePlusCommissionEmployee4 get method to access private instance variable.
98
Outline Use BasePlusCommissionEmployee4 set method to modify private instance variable baseSalary. BasePlusCommission EmployeeTest4.java (2 of 2)
99
Class CommissionEmployee3
4 public class CommissionEmployee3 5 { 6 private String firstName; 7 private String lastName; 8 private String socialSecurityNumber; 9 private double grossSales; // gross weekly sales 10 private double commissionRate; // commission percentage 11 12 // five-argument constructor 13 public CommissionEmployee3( String first, String last, String ssn, double sales, double rate ) 15 { // implicit call to Object constructor occurs here firstName = first; lastName = last; socialSecurityNumber = ssn; setGrossSales( sales ); // validate and store gross sales setCommissionRate( rate ); // validate and store commission rate 22 } // end five-argument CommissionEmployee3 constructor
100
Class CommissionEmployee3 (cont)
24 // set first name 25 public void setFirstName( String first ) 26 { firstName = first; 28 } // end method setFirstName // return first name 31 public String getFirstName() 32 { return firstName; 34 } // end method getFirstName 35 36 // set last name 37 public void setLastName( String last ) 38 { lastName = last; 40 } // end method setLastName 41 42 // return last name 43 public String getLastName() 44 { return lastName; 46 } // end method getLastName
101
Class CommissionEmployee3 (cont)
48 // set social security number 49 public void setSocialSecurityNumber( String ssn ) 50 { socialSecurityNumber = ssn; // should validate 52 } // end method setSocialSecurityNumber 53 54 // return social security number 55 public String getSocialSecurityNumber() 56 { return socialSecurityNumber; 58 } // end method getSocialSecurityNumber 59 60 // set gross sales amount 61 public void setGrossSales( double sales ) 62 { grossSales = ( sales < 0.0 ) ? 0.0 : sales; 64 } // end method setGrossSales 65 66 // return gross sales amount 67 public double getGrossSales() 68 { return grossSales; 70 } // end method getGrossSales
102
Class CommissionEmployee3 (cont)
73 public void setCommissionRate( double rate ) 74 { commissionRate = ( rate > 0.0 && rate < 1.0 ) ? rate : 0.0; 76 } // end method setCommissionRate 77 78 // return commission rate 79 public double getCommissionRate() 80 { return commissionRate; 82 } // end method getCommissionRate 84 // calculate earnings 85 public double earnings() 86 { return getCommissionRate() * getGrossSales(); 88 } // end method earnings
103
Class CommissionEmployee3 (cont)
90 // return String representation of CommissionEmployee3 object 91 public String toString() 92 { 93 return String.format( "%s: %s %s\n%s: %s\n%s: %.2f\n%s: %.2f", "commission employee", getFirstName(), getLastName(), "social security number", getSocialSecurityNumber(), "gross sales", getGrossSales(), "commission rate", getCommissionRate() ); 98 } // end method toString 99 } // end class CommissionEmployee3
104
Class BasePlusCommissionEmployee4
6 public class BasePlusCommissionEmployee4 extends CommissionEmployee3 7 { 8 private double baseSalary; // base salary per week 9 10 // six-argument constructor 11 public BasePlusCommissionEmployee4( String first, String last, String ssn, double sales, double rate, double salary ) 13 { super( first, last, ssn, sales, rate ); setBaseSalary( salary ); // validate and store base salary 16 } // end six-argument BasePlusCommissionEmployee4 constructor 17 18 // set base salary 19 public void setBaseSalary( double salary ) 20 { baseSalary = ( salary < 0.0 ) ? 0.0 : salary; 22 } // end method setBaseSalary
105
Class BasePlusCommissionEmployee4 (cont)
24 // return base salary 25 public double getBaseSalary() 26 { return baseSalary; 28 } // end method getBaseSalary 29 30 // calculate earnings 31 public double earnings() 32 { return getBaseSalary() + super.earnings(); 34 } // end method earnings 35 36 // return String representation of BasePlusCommissionEmployee4 37 public String toString() 38 { return String.format( "%s %s\n%s: %.2f", "base-salaried", super.toString(), "base salary", getBaseSalary() ); 41 } // end method toString 42 } // end class BasePlusCommissionEmployee4
106
Note That The earnings method of BasePlusCommissionEmployee4 calls the earnings method of CommissionEmployee3 Othewise part of the code is repeted Similarly toString method of BasePlusCommissionEmployee4 calls the toString method of CommissionEmployee3
107
Test Class - BasePlusCommissionEmployeeTest4
4 public class BasePlusCommissionEmployeeTest4 5 { 6 public static void main( String args[] ) 7 { // instantiate BasePlusCommissionEmployee4 object BasePlusCommissionEmployee4 employee = new BasePlusCommissionEmployee4( "Bob", "Lewis", " ", 5000, .04, 300 ); 12 // get base-salaried commission employee data System.out.println( "Employee information obtained by get methods: \n" ); System.out.printf( "%s %s\n", "First name is", employee.getFirstName() ); System.out.printf( "%s %s\n", "Last name is", employee.getLastName() ); System.out.printf( "%s %s\n", "Social security number is", employee.getSocialSecurityNumber() ); System.out.printf( "%s %.2f\n", "Gross sales is", employee.getGrossSales() );
108
Test Class - BasePlusCommissionEmployeeTest4 (cont)
System.out.printf( "%s %.2f\n", "Commission rate is", employee.getCommissionRate() ); System.out.printf( "%s %.2f\n", "Base salary is", employee.getBaseSalary() ); 28 employee.setBaseSalary( 1000 ); // set base salary 30 System.out.printf( "\n%s:\n\n%s\n", "Updated employee information obtained by toString", employee.toString() ); 34 } // end main 35 } // end class BasePlusCommissionEmployeeTest4
109
Output - BasePlusCommissionEmployeeTest4
Employee information obtained by get methods: First name is Bob Last name is Lewis Social security number is Gross sales is Commission rate is 0.04 Base salary is Updated employee information obtained by toString: base-salaried commission employee: Bob Lewis social security number: gross sales: commission rate: 0.04 base salary:
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: CommissionEmployee3-BasePlusCommissionEmployee4 hierarchy CommissionEmployee3 constructor called second last (last is Object constructor) CommissionEmployee3 constructor’s body finishes execution second (first is Object constructor’s body)
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…)
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).
113
Class CommissionEmployee
public class CommissionEmployee { protected String firstName; protected double grossSales; // gross weekly sales protected double commissionRate; // commission percentage // five-argument constructor public CommissionEmployee( String first, double sales, double rate ) // implicit call to Object constructor occurs here firstName = first; setGrossSales( sales ); // validate and store gross sales setCommissionRate( rate ); // validate and store commission rate System.out.printf( "\nCommissionEmployee constructor:\n%s\n", this ); // optionally this.toString or just toString } // end CommissionEmployee constructor
114
Class CommissionEmployee (cont)
// set first name public void setFirstName( String first ) { firstName = first; } // end method setFirstName // return first name public String getFirstName() return firstName; } // end method getFirstName
115
Class CommissionEmployee (cont)
// set gross sales amount public void setGrossSales( double sales ) { grossSales = ( sales < 0.0 ) ? 0.0 : sales; } // end method setGrossSales // return gross sales amount public double getGrossSales() return grossSales; } // end method getGrossSales
116
Class CommissionEmployee (cont)
// set commission rate public void setCommissionRate( double rate ) { commissionRate = ( rate > 0.0 && rate < 1.0 ) ? rate : 0.0; } // end method setCommissionRate // return commission rate public double getCommissionRate() return commissionRate; } // end method getCommissionRate // calculate earnings public double earnings() return commissionRate * grossSales; } // end method earnings
117
Class CommissionEmployee (cont)
// return String representation of CommissionEmployee2 object public String toString() { return String.format( "%s: %s\n%s: %.2f\n%s: %.2f", "commission employee", firstName, "gross sales", grossSales, "commission rate", commissionRate ); } // end method toString } // end class CommissionEmployee2
118
Class BasePlusCommissionEmployee
public class BasePlusCommissionEmployee extends CommissionEmployee { private double baseSalary; // base salary per week // constructor public BasePlusCommissionEmployee( String first, double sales, double rate, double salary ) super( first, sales, rate ); setBaseSalary( salary ); // validate and store base salary System.out.printf( "\nBasePlusCommissionEmployee constructor:\n%s\n", this ); } // end six-argument BasePlusCommissionEmployee3 constructor
119
Class BasePlusCommissionEmployee (cont)
constructor // set base salary public void setBaseSalary( double salary ) { baseSalary = ( salary < 0.0 ) ? 0.0 : salary; } // end method setBaseSalary // return base salary public double getBaseSalary() return baseSalary; } // end method getBaseSalary
120
Class BasePlusCommissionEmployee (cont)
// calculate earnings public double earnings() { return baseSalary + super.earnings(); } // end method earnings // return String representation of BasePlusCommissionEmployee3 public String toString() return String.format( "%s %s\n%s: %.2f", "base-salaried“, super.toString(), "base salary", baseSalary); } // end method toString } // end class BasePlusCommissionEmployee3
121
Class ConstructorTest
public class ConstructorTest { public static void main( String args[] ) CommissionEmployee employee1 = new CommissionEmployee( "Bob", 5000, .04 ); System.out.println(); BasePlusCommissionEmployee employee2 = new BasePlusCommissionEmployee( "Lisa", 2000, .06, 800 ); BasePlusCommissionEmployee employee3 = "Mark", 8000, .15, 2000 ); } // end main } // end class ConstructorTest
122
output CommissionEmployee constructor: commission employee: Bob
gross sales: commission rate: 0.04 base-salaried commission employee: Lisa gross sales: commission rate: 0.06 base salary: 0.00 BasePlusCommissionEmployee constructor: base salary:
123
output CommissionEmployee constructor:
base-salaried commission employee: Mark gross sales: commission rate: 0.15 base salary: 0.00 BasePlusCommissionEmployee constructor: base salary:
124
Outline (1 of 4) Lines 23-24 CommissionEmployee4.java
Constructor outputs message to demonstrate method call order.
125
Outline CommissionEmployee4.java (2 of 4)
126
Outline CommissionEmployee4.java (3 of 4)
127
Outline CommissionEmployee4.java (4 of 4)
128
Outline (1 of 2) Lines 15-16 BasePlusCommissionEmployee5.java
Constructor outputs message to demonstrate method call order.
129
Outline BasePlusCommissionEmployee5.java (2 of 2)
130
Outline (1 of 2) Lines 8-9 Lines 12-19 ConstructorTest .java
Instantiate CommissionEmployee4 object ConstructorTest .java (1 of 2) Lines 8-9 Lines 12-19 Instantiate two BasePlusCommissionEmployee5 objects to demonstrate order of subclass and superclass constructor method calls.
131
Outline (2 of 2) ConstructorTest .java
Subclass BasePlusCommissionEmployee5 constructor body executes after superclass CommissionEmployee4’s constructor finishes execution.
132
Note That The earning method of BasePlusCommissionEmployee3
There is still some repeated code Part of the code is the same as CommissionEmployee2’s code Similarly: toString method most of the code of the toString method of CommissionEmployee2 is repeated
133
Class CommissionEmployee4 (cont)
27 // set first name 28 public void setFirstName( String first ) 29 { firstName = first; 31 } // end method setFirstName 32 33 // return first name 34 public String getFirstName() 35 { return firstName; 37 } // end method getFirstName 38 39 // set last name 40 public void setLastName( String last ) 41 { lastName = last; 43 } // end method setLastName 44 45 // return last name 46 public String getLastName() 47 { return lastName; 49 } // end method getLastName
134
Class CommissionEmployee4 (cont)
51 // set social security number 52 public void setSocialSecurityNumber( String ssn ) 53 { socialSecurityNumber = ssn; // should validate 55 } // end method setSocialSecurityNumber 56 57 // return social security number 58 public String getSocialSecurityNumber() 59 { return socialSecurityNumber; 61 } // end method getSocialSecurityNumber 62 63 // set gross sales amount 64 public void setGrossSales( double sales ) 65 { grossSales = ( sales < 0.0 ) ? 0.0 : sales; 67 } // end method setGrossSales 68 69 // return gross sales amount 70 public double getGrossSales() 71 { return grossSales; 73 } // end method getGrossSales
135
Class CommissionEmployee4 (cont)
75 // set commission rate 76 public void setCommissionRate( double rate ) 77 { commissionRate = ( rate > 0.0 && rate < 1.0 ) ? rate : 0.0; 79 } // end method setCommissionRate 74 81 // return commission rate 82 public double getCommissionRate() 83 { return commissionRate; 85 } // end method getCommissionRate
136
Class CommissionEmployee4 (cont)
87 // calculate earnings 88 public double earnings() 89 { return getCommissionRate() * getGrossSales(); 91 } // end method earnings 93// return String representation of CommissionEmployee4 object 94 public String toString() 95 { 96 return String.format( "%s: %s %s\n%s: %s\n%s: %.2f\n%s: %. "commission employee", getFirstName(), getLastName(), "social security number", getSocialSecurityNumber(), "gross sales", getGrossSales(), "commission rate", getCommissionRate() ); } // end method toString 102 } // end class CommissionEmployee4
137
Class BasePlusCommissionEmployee5
4 public class BasePlusCommissionEmployee5 extends CommissionEmployee4 5 { 6 private double baseSalary; // base salary per week 7 8 // six-argument constructor 9 public BasePlusCommissionEmployee5( String first, String last, String ssn, double sales, double rate, double salary ) 11 { super( first, last, ssn, sales, rate ); setBaseSalary( salary ); // validate and store base salary 14 System.out.printf( "\nBasePlusCommissionEmployee5 constructor:\n%s\n", this ); 17 } // end six-argument BasePlusCommissionEmployee5 constructor 18 19 // set base salary 20 public void setBaseSalary( double salary ) 21 { baseSalary = ( salary < 0.0 ) ? 0.0 : salary; 23 } // end method setBaseSalary
138
Class BasePlusCommissionEmployee5 (cont)
25 // return base salary 26 public double getBaseSalary() 27 { return baseSalary; 29 } // end method getBaseSalary 30 31 // calculate earnings 32 public double earnings() 33 { return getBaseSalary() + super.earnings(); 35 } // end method earnings 36 37 // return String representation of BasePlusCommissionEmployee5 38 public String toString() 39 { return String.format( "%s %s\n%s: %.2f", "base-salaried", super.toString(), "base salary", getBaseSalary() ); 42 } // end method toString 43 } // end class BasePlusCommissionEmployee5
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
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.
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.
142
Software Engineering Observation 4.11
Declaring a subclass does not affect its superclass’s source code. Inheritance preserves the integrity of the superclass.
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.
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.
145
4.7 Object Class Class Object methods clone equals finalize getClass
hashCode notify, notifyAll, wait toString
146
Fig. 9.18 | Object methods that are inherited directly or indirectly by all classes. (Part 1 of 4)
147
Fig. 9.18 | Object methods that are inherited directly or indirectly by all classes. (Part 2 of 4)
148
Fig. 9.18 | Object methods that are inherited directly or indirectly by all classes. (Part 3 of 4)
149
Fig. 9.18 | Object methods that are inherited directly or indirectly by all classes. (Part 4 of 4)
150
Example – University Members
Person Students GradStudent Person Staff Faculty
151
The Person Class Person Attrubutes: Behavior name
Others: lastName,SSN,birthDate,… Behavior toString, other display methods Other get and set methods
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 toString, other display methods status: normal or repeating if gpa < 2.0 then “repeating” else “normal” Other get and set methods
153
The GraduagteStudent Class
GraduateStudent Attrubutes: Thesis title Others: graduated university Attributes: claswide Onprobesion limit: applied to all graduate students Behavior toString, other display methods status: normal or onprobesion if gpa < 2.5 then “onprobesion” else “normal” Other get and set methods
154
The Person Class implementation
public class Person { protected String name; public Person(String name) { this.name = name; } // end of constructor Person public String toString() { return String.format(“%10s:%20s\n”, “Name”,name); } // end of method } // end of class
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); setGpa(gpa); } // end of constructor Student public void setGpa(double gpa ) { this.gpa = (gpa >=0.0 && gpa <= 4.0) ? gpa : 0.0; } // end of methd
156
The Student Class implementation
private String getStatus() { return ( gpa >= REP_LIMIT) ? “normal” ; “repeating”; } // end of method public String toString() { return String.format(“%s%10s:%20s\n”, super.toString(), “Status”,getStatus()); } // end of class
157
The GradStudent Class implementation
public class GradStudent extends Student { private String thesis; private static final double ONPR_LIMIT = 2.5; public GraduateStudent( String name , double gpa , String thesis ) { super(name,gpa); this.thesis = thesis; } // end of constructor GradStudent
158
The GradStudent Class implementation
private String getStatus() { return ( gpa >= ONPR_LIMIT) ? “normal” ; , “onprobesion”; } // end of method public String toString() { return String.format(“%s%10s:%20s\n”, super.toString(), “Thesis”,thesis); } // end of class
159
Test Class Inplementation
public class Test { public static void main(String args[]) // create a Student and a gradStudent object Student ali = new Student(“ali”,2.8); GradStudent ahmet = new GradStudent(“ahmet”,2.3,”MIS in Turkey”); // display students System.out.printf(“%s\n%s”,ali, ahmet.toString()); } // end of main } // end of Test class
160
Output Name: ali Status: normal Name: ahmet Status: onprob
Thesis: MIS in Turkey
161
Explanation ali’s toString method is invoked which calls the toString method of Person to display name and then displays status of the Student object as normal ahmet’s toString method is invoked which calls the toString method of Student After a call to super’s toString to display name, the toString method calls the status method.
162
Explanation (cont.) Which status method is called? –
The Student’s or CraduateStudnet’s Although it is invoked in the pease of code of Student class’s toString, type of the object determines which method wil be invoked. Since the object refered by ahmet is of type GraduateStudent, no mather at what point in the class hierarchy it is invoked always the GraduateStudent’s status method is executed.
163
Example – Bank Classes In a bank different types of bank accounts
inherited from accout class Account Saving account,
164
Example – Bank Classes Account class has İnstance variables Behavior:
Name:String Balance:double Code:String Behavior: Deposit Withdraw getBalance getAccCode
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
166
Example – Bank Classes A simple total interest rate calculation would be public double calculateInterest() { return balance*(1+interestRate); } Here interstRate is a static variable of SavingAccount class Balanc is a instance variable of the Account class inherited by SavingAccount
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
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+intRate)2
169
Example – Bank Classes Whenever an oppoertatin is made to an SavingAccout first calculate the total new balnace WholeTerms = (currentMonth-monthLastUpdate)/term; balance = balance*Math.pow(1+intRate,wholeTerms);
170
Example – Bank Classes 8 end of first term 13 current month
5:lsat update 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
171
At the end of each opperation update the lastUpdatedMonth variable
Suppose customer withdraw money on month 10 After the withdraw opperation make lastUpdatedMonth of the SavingAccount to the currentMonth
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.