0% found this document useful (0 votes)
5 views28 pages

CSC245 Lecture 3

The document provides an overview of inheritance in object-oriented programming, explaining the concepts of superclasses and subclasses, the is-a relationship, and the access levels of class members. It includes examples of class hierarchies and discusses the creation of classes using inheritance, specifically focusing on CommissionEmployee and BasePlusCommissionEmployee. Additionally, it highlights the importance of method overriding and the implications of access modifiers in subclassing.

Uploaded by

armajd676
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views28 pages

CSC245 Lecture 3

The document provides an overview of inheritance in object-oriented programming, explaining the concepts of superclasses and subclasses, the is-a relationship, and the access levels of class members. It includes examples of class hierarchies and discusses the creation of classes using inheritance, specifically focusing on CommissionEmployee and BasePlusCommissionEmployee. Additionally, it highlights the importance of method overriding and the implications of access modifiers in subclassing.

Uploaded by

armajd676
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Object-Oriented

Programming: Inheritance

01/24/2019 DIANA HAIDAR 1


Outline
 Introduction to Inheritance
 Superclasses and Subclasses
 protected Members
 Relationship between Superclasses and Subclasses
 1. Creating and Using a CommisionEmployee class
 2. Creating a BasePlusCommisionEmployee class without using Inheritance
 3. CommissionEmployee - BasePlusCommisionEmployee Inheritance Hierarchy using protected Instance
Variables
 Software Engineering with Inheritance

01/24/2019 DIANA HAIDAR 2


Introduction to Inheritance
 Software reusability:
Inheritance is a form of software reuse in OOP in which a new class is created by absorbing an existing
class's members and enhancing them with new or modified capabilities.
 The existing class is called the superclass, and the new class is the subclass.
 A subclass extends a superclass.
 A subclass is more specific than its superclass and represents a more specialized group of objects.
 The subclass exhibits the behaviors of its superclass and additional behaviors that are specific to the
subclass.

01/24/2019 DIANA HAIDAR 3


Introduction to Inheritance
 Types of superclass in class hierarchy:
 Direct superclass is the superclass which the subclass explicitly inherits (one level up hierarchy).
 Indirect superclass is any class above the direct superclass in the class hierarchy (more than one level up
hierarchy).
 The class Object (package java.lang) is the superclass (root) of every class in the class hierarchy in Java.
Every class directly or indirectly extends (inherits from) the class Object.
 Cases of class inheritance:
 Single inheritance which occurs when a subclass is derived from one direct superclass.
 Multiple inheritance which occurs when a subclass is derived from more than one direct superclass.
 Java, unlike C++, does not support multiple inheritance. Instead, Java programmers can use multiple
interfaces to realize many of the benefits of multiple inheritance.

01/24/2019 DIANA HAIDAR 4


Superclasses and Subclasses
 Is-a relationship represents inheritance, where an object of a subclass is-an object of its superclass.
Recall: has-a relationship represents composition (Session 2).
 Example: Rectangle is-a Quadrilateral .
class Rectangle inherits from class Quadrilateral.
 Rectangle is a subclass.
 Quadrilateral is a superclass.

 A superclass can have many subclasses.


 The set of objects represented by a superclass is typically less than the set of objects represented by any
of its subclasses.
 Example: The superclass Vehicle represents all vehicles, including cars, trucks, boats, bicycles, etc.
On the other hand, the subclass Car represents a smaller, more specific subset of vehicles.

01/24/2019 DIANA HAIDAR 5


Superclasses and subclasses
Inheritance examples:

Superclass Subclasses
Student GraduateStudent, UndergraduateStudent
Shape Circle, Triangle, Rectangle
Loan CarLoan, HomeImprovementLoan,
MortgageLoan
Employee Faculty, Staff
BankAccount CheckingAccount, SavingsAccount

01/24/2019 DIANA HAIDAR 6


Superclasses and subclasses
 Inheritance relationships form tree-like hierarchical structures; a superclass exists in a hierarchical
relationship with its subclasses.
 When a class participates in inheritance relationship, a class becomes:
 a superclass supplying members to other subclasses;
 a subclass inheriting its members from other classes; or
 both a superclass and a subclass.

01/24/2019 DIANA HAIDAR 7


Superclasses and subclasses
Example of Inheritance hierarchy for class CommunityMember:

01/24/2019 DIANA HAIDAR 8


Superclasses and subclasses
Example of Inheritance hierarchy for class Shape:

01/24/2019 DIANA HAIDAR 9


protected Members
 public class members are accessible wherever the program has a reference to an object of that class or
one of its subclasses.
 private class members are accessible ONLY from within the class itself, but not inherited by its subclasses.
 protected class members offers intermediate level of access between public and private.
 A superclass's protected members can be accessed by:
 members of that superclass;
 members of its subclasses; and
 members of other classes in the same package (i.e. protected members also have package access).

01/24/2019 DIANA HAIDAR 10


protected Members
 All public and protected class members retain their original access modifier when they become members
of the subclass.
 When a subclass method overrides a superclass method, the superclass method can be accessed by
preceding the superclass method name with keyword super and a dot (.) separator.
 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.

01/24/2019 DIANA HAIDAR 11


Relationship between Superclasses and Subclasses
 Case Study - Inheritance hierarchy containing types of employees in a company's payroll application
 Superclass: class CommissionEmployee where commission employees are paid a percentage of their
sales.
 private instance variables: firstName, lastName, socialSecurityNumber, grossSales, commissionRate.

 Subclass: BasePlusCommissionEmployee where base-salaried commission employees receive a base


salary plus a percentage of their sales.
 private instance variables: firstName, lastName, socialSecurityNumber, grossSales, commissionRate,
baseSalary.

01/24/2019 DIANA HAIDAR 12


1. Creating and Using a CommisionEmployee class
 class CommissionEmployee extends (i.e. inherits from) class Object (package java.lang).
 Java programmers use inheritance to create classes from existing classes.
 Every class in Java (except class Object) inherits from an existing class.
 Every class inherits the methods of class Object (e.g. clone, equals, finalize, toString, etc.).
 If a class does not specify that it extends another class, this class implicitly extends class Object.

01/24/2019 DIANA HAIDAR 13


1. Creating and Using a CommisionEmployee class 1 // Fig. 9.4: CommissionEmployee.java
2 // CommissionEmployee class represents a commission employee.
 Constructors are not inherited, so class 3
CommissionEmployee does not inherit 4 public class CommissionEmployee extends Object
5 {
class Object's constructor. 6 private String firstName;
7 private String lastName;
 class CommissionEmployee's constructor 8 private String socialSecurityNumber;
calls class Object's constructor implicitly. 9 private double grossSales; // gross weekly sales
10 private double commissionRate; // commission percentage
 The first task of any subclass constructor is 11
to call its direct superclass's constructor, 12 // five-argument constructor
13 public CommissionEmployee( String first, String last, String ssn,
either explicitly or implicitly (if no
14 double sales, double rate )
constructor call is specified), to ensure that 15 {
the instance variables inherited from the 16 // implicit call to Object constructor occurs here
superclass are initialized properly. 17 firstName = first;
18 lastName = last;
19 socialSecurityNumber = ssn;
20 setGrossSales( sales ); // validate and store gross sales
21 setCommissionRate( rate ); // validate and store commission rate
22 } // end five-argument CommissionEmployee constructor
23
24 // set first name
25 public void setFirstName( String first )
26 {
27 firstName = first;
28 } // end method setFirstName
29
01/24/2019 DIANA HAIDAR 14
30
1. Creating and Using a CommisionEmployee class
// return first name 60 // set gross sales amount
31 public String getFirstName() 61 public void setGrossSales( double sales )
32 { 62 {
33 return firstName; 63 grossSales = ( sales < 0.0 ) ? 0.0 : sales;
34 } // end method getFirstName 64 } // end method setGrossSales
35 65
36 // set last name 66 // return gross sales amount
37 public void setLastName( String last ) 67 public double getGrossSales()
38 { 68 {
39 lastName = last; 69 return grossSales;
40 } // end method setLastName 70 } // end method getGrossSales
41 71
42 // return last name 72 // set commission rate
43 public String getLastName() 73 public void setCommissionRate( double rate )
74 {
44 {
75 commissionRate = ( rate > 0.0 && rate < 1.0 ) ? rate : 0.0;
45 return lastName;
76 } // end method setCommissionRate
46 } // end method getLastName
77
47
78 // return commission rate
48 // set social security number
79 public double getCommissionRate()
49 public void setSocialSecurityNumber( String ssn )
80 {
50 { 81 return commissionRate;
51 socialSecurityNumber = ssn; // should validate 82 } // end method getCommissionRate
52 } // end method setSocialSecurityNumber 83
53 84 // calculate earnings
54 // return social security number 85 public double earnings()
55 public String getSocialSecurityNumber() 86 {
56 { 87 return commissionRate * grossSales;
57 return socialSecurityNumber; 88 } // end method earnings
58 } // end method getSocialSecurityNumber 89
59
01/24/2019 DIANA HAIDAR 15
1. Creating and Using a CommisionEmployee class
 Method toString of class CommissionEmployee overrides (redefines) class Object's toString method.
 To override a superclass method, a subclass method must declare a method with the same signature
(method name, number of parameters, and parameter types) as the superclass method.
 Common programming error: 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.
 Doing so would break the is-a relationship.
 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.
90 // return String representation of CommissionEmployee object
91 public String toString()
92 {
93 return String.format( "%s: %s %s\n%s: %s\n%s: %.2f\n%s: %.2f",
94 "commission employee", firstName, lastName,
95 "social security number", socialSecurityNumber,
96 "gross sales", grossSales,
97 "commission rate", commissionRate );
98 } // end method toString
99 } // end class CommissionEmployee

01/24/2019 DIANA HAIDAR 16


1. Creating and Using a CommisionEmployee class
1 // Fig. 9.5: CommissionEmployeeTest.java
29 System.out.printf( "\n%s:\n\n%s\n",
2 // Testing class CommissionEmployee.
30 "Updated employee information obtained by toString", employee );
3
4 public class CommissionEmployeeTest 31 } // end main
5 { 32 } // end class CommissionEmployeeTest
6 public static void main( String args[] )
Employee information obtained by get methods:
7 {
8 // instantiate CommissionEmployee object First name is Sue
Last name is Jones
9 CommissionEmployee employee = new CommissionEmployee( Social security number is 222-22-2222
10 "Sue", "Jones", "222-22-2222", 10000, .06 ); Gross sales is 10000.00
11 Commission rate is 0.06
12 // get commission employee data Updated employee information obtained by toString:
13 System.out.println(
commission employee: Sue Jones
14 "Employee information obtained by get methods: \n" ); social security number: 222-22-2222
15 System.out.printf( "%s %s\n", "First name is", gross sales: 500.00
commission rate: 0.10
16 employee.getFirstName() );
17 System.out.printf( "%s %s\n", "Last name is",
18 employee.getLastName() );
19 System.out.printf( "%s %s\n", "Social security number is",
20 employee.getSocialSecurityNumber() );
21 System.out.printf( "%s %.2f\n", "Gross sales is",
22 employee.getGrossSales() );
23 System.out.printf( "%s %.2f\n", "Commission rate is",
24 employee.getCommissionRate() );
25
26 employee.setGrossSales( 500 ); // set gross sales
27 employee.setCommissionRate( .1 ); // set commission rate
28

01/24/2019 DIANA HAIDAR 17


2. Creating a BasePlusCommisionEmployee class without using Inheritance
1 // Fig. 9.6: BasePlusCommissionEmployee.java

 Much of the code of class 2 // BasePlusCommissionEmployee class represents an employee that receives
3 // a base salary in addition to commission.
BasePlusCommissionEmployee is similar 4
(if not identical) to the code of class 5 public class BasePlusCommissionEmployee
CommissionEmployee. 6 {
7 private String firstName;
 Similarities: 8 private String lastName;

 private instance variables 9 private String socialSecurityNumber;


10 private double grossSales; // gross weekly sales
 Constructor 11 private double commissionRate; // commission percentage
12 private double baseSalary; // base salary per week
 public methods 13
14 // six-argument constructor
 Additions: 15 public BasePlusCommissionEmployee( String first, String last,
 private instance variable baseSalary 16 String ssn, double sales, double rate, double salary )
17 {
 method setBaseSalary and method 18 // implicit call to Object constructor occurs here
getBaseSalary 19 firstName = first;
20 lastName = last;
21 socialSecurityNumber = ssn;
22 setGrossSales( sales ); // validate and store gross sales
23 setCommissionRate( rate ); // validate and store commission rate
24 setBaseSalary( salary ); // validate and store base salary
25 } // end six-argument BasePlusCommissionEmployee constructor
26

01/24/2019 DIANA HAIDAR 18


2. Creating a BasePlusCommisionEmployee class without using Inheritance
27 // set first name 57 // return social security number
28 public void setFirstName( String first ) 58 public String getSocialSecurityNumber()
29 { 59 {
30 firstName = first; 60 return socialSecurityNumber;
31 } // end method setFirstName 61 } // end method getSocialSecurityNumber
32 62
33 // return first name 63 // set gross sales amount
34 public String getFirstName() 64 public void setGrossSales( double sales )
35 { 65 {
36 return firstName; 66 grossSales = ( sales < 0.0 ) ? 0.0 : sales;
37 } // end method getFirstName 67 } // end method setGrossSales
38 68
39 // set last name 69 // return gross sales amount
40 public void setLastName( String last ) 70 public double getGrossSales()
41 { 71 {
42 lastName = last; 72 return grossSales;
43 } // end method setLastName 73 } // end method getGrossSales
44 74
45 // return last name 75 // set commission rate
46 public String getLastName() 76 public void setCommissionRate( double rate )
47 { 77 {
48 return lastName; 78 commissionRate = ( rate > 0.0 && rate < 1.0 ) ? rate : 0.0;
49 } // end method getLastName 79 } // end method setCommissionRate
50 80
51 // set social security number 81 // return commission rate
52 public void setSocialSecurityNumber( String ssn ) 82 public double getCommissionRate()
53 { 83 {
54 socialSecurityNumber = ssn; // should validate 84 return commissionRate;
55 } // end method setSocialSecurityNumber 85 } // end method getCommissionRate
56 86
01/24/2019 DIANA HAIDAR 19
2. Creating a BasePlusCommisionEmployee class without using Inheritance
87 // set base salary
88 public void setBaseSalary( double salary )
89 {
90 baseSalary = ( salary < 0.0 ) ? 0.0 : salary;
91 } // end method setBaseSalary
92
93 // return base salary
94 public double getBaseSalary()
95 {
96 return baseSalary;
97 } // end method getBaseSalary
98
99 // calculate earnings
100 public double earnings()
101 {
102 return baseSalary + ( commissionRate * grossSales );
103 } // end method earnings
104
105 // 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
01/24/2019 DIANA HAIDAR 20
2. Creating a BasePlusCommisionEmployee class without using Inheritance
1 // Fig. 9.7: BasePlusCommissionEmployeeTest.java 29 employee.setBaseSalary( 1000 ); // set base salary
2 // Testing class BasePlusCommissionEmployee. 30
3 31 System.out.printf( "\n%s:\n\n%s\n",
4 public class BasePlusCommissionEmployeeTest 32 "Updated employee information obtained by toString",
5 { 33 employee.toString() );
6 public static void main( String args[] ) 34 } // end main
7 { 35 } // end class BasePlusCommissionEmployeeTest
8 // instantiate BasePlusCommissionEmployee object
9 BasePlusCommissionEmployee employee = Employee information obtained by get methods:
10 new BasePlusCommissionEmployee( First name is Bob
11 "Bob", "Lewis", "333-33-3333", 5000, .04, 300 ); Last name is Lewis
Social security number is 333-33-3333
12 Gross sales is 5000.00
Commission rate is 0.04
13 // get base-salaried commission employee data Base salary is 300.00
14 System.out.println(
Updated employee information obtained by toString:
15 "Employee information obtained by get methods: \n" );
16 System.out.printf( "%s %s\n", "First name is", base-salaried commission employee: Bob Lewis
social security number: 333-33-3333
17 employee.getFirstName() ); gross sales: 5000.00
18 System.out.printf( "%s %s\n", "Last name is", commission rate: 0.04
base salary: 1000.00
19 employee.getLastName() );
20 System.out.printf( "%s %s\n", "Social security number is",
21 employee.getSocialSecurityNumber() );
22 System.out.printf( "%s %.2f\n", "Gross sales is",
23 employee.getGrossSales() );
24 System.out.printf( "%s %.2f\n", "Commission rate is",
25 employee.getCommissionRate() );
26 System.out.printf( "%s %.2f\n", "Base salary is",
27 employee.getBaseSalary() );
28
01/24/2019 DIANA HAIDAR 21
3. CommissionEmployee - BasePlusCommisionEmployee Inheritance Hierarchy
using protected Instance Variables
 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.
 With inheritance, the common instance variables and methods of all the classes in the hierarchy are
declared in the superclass.
 Use the protected access modifier to enable the access of the objects of ONLY the subclass (or other
classes in the package) to the common instance variables and methods declared in the superclass.

01/24/2019 DIANA HAIDAR 22


3. CommissionEmployee - BasePlusCommisionEmployee Inheritance Hierarchy
using protected Instance Variables
1 // Fig. 9.9: CommissionEmployee2.java 30 // return first name
2 // CommissionEmployee2 class represents a commission employee. 31 public String getFirstName()
3 32 {
4 public class CommissionEmployee2 33 return firstName;
5 { 34 } // end method getFirstName
6 protected String firstName; 35
7 protected String lastName; 36 // set last name
8 protected String socialSecurityNumber; 37 public void setLastName( String last )
9 protected double grossSales; // gross weekly sales 38 {
10 protected double commissionRate; // commission percentage 39 lastName = last;
11 40 } // end method setLastName
41
12 // five-argument constructor
42 // return last name
13 public CommissionEmployee2( String first, String last, String ssn,
43 public String getLastName()
14 double sales, double rate )
44 {
15 {
45 return lastName;
16 // implicit call to Object constructor occurs here
46 } // end method getLastName
17 firstName = first;
47
18 lastName = last;
48 // set social security number
19 socialSecurityNumber = ssn;
49 public void setSocialSecurityNumber( String ssn )
20 setGrossSales( sales ); // validate and store gross sales 50 {
21 setCommissionRate( rate ); // validate and store commission rate 51 socialSecurityNumber = ssn; // should validate
22 } // end five-argument CommissionEmployee2 constructor 52 } // end method setSocialSecurityNumber
23 53
24 // set first name 54 // return social security number
25 public void setFirstName( String first ) 55 public String getSocialSecurityNumber()
26 { 56 {
27 firstName = first; 57 return socialSecurityNumber;
28 } // end method setFirstName 58 } // end method getSocialSecurityNumber
29 59
01/24/2019 DIANA HAIDAR 23
3. CommissionEmployee - BasePlusCommisionEmployee Inheritance Hierarchy using
protected Instance Variables
60 // set gross sales amount 90 // return String representation of CommissionEmployee2 object
61 public void setGrossSales( double sales )
91 public String toString()
62 {
92 {
63 grossSales = ( sales < 0.0 ) ? 0.0 : sales;
64 } // end method setGrossSales 93 return String.format( "%s: %s %s\n%s: %s\n%s: %.2f\n%s: %.2f",
65 94 "commission employee", firstName, lastName,
66 // return gross sales amount 95 "social security number", socialSecurityNumber,
67 public double getGrossSales() 96 "gross sales", grossSales,
68 {
97 "commission rate", commissionRate );
69 return grossSales;
70 } // end method getGrossSales
98 } // end method toString
71 99 } // end class CommissionEmployee2
72 // set commission rate
73 public void setCommissionRate( double rate )
74 {
75 commissionRate = ( rate > 0.0 && rate < 1.0 ) ? rate : 0.0;
76 } // end method setCommissionRate
77
78 // return commission rate
79 public double getCommissionRate()
80 {
81 return commissionRate;
82 } // end method getCommissionRate
83
84 // calculate earnings
85 public double earnings()
86 {
87 return commissionRate * grossSales;
88 } // end method earnings
89
01/24/2019 DIANA HAIDAR 24
3. CommissionEmployee - BasePlusCommisionEmployee Inheritance Hierarchy using
protected Instance Variables
1 // Fig. 9.10: BasePlusCommissionEmployee3.java 29 // calculate earnings
2 // BasePlusCommissionEmployee3 inherits from CommissionEmployee2 and has 30 public double earnings()
3 // access to CommissionEmployee2's protected members. 31 {
4 32 return baseSalary + ( commissionRate * grossSales );
5 public class BasePlusCommissionEmployee3 extends CommissionEmployee2 33 } // end method earnings
6 {
34
7 private double baseSalary; // base salary per week
35 // return String representation of BasePlusCommissionEmployee3
8
36 public String toString()
9 // six-argument constructor
37 {
10 public BasePlusCommissionEmployee3( String first, String last,
11 String ssn, double sales, double rate, double salary ) 38 return String.format(
12 { 39 "%s: %s %s\n%s: %s\n%s: %.2f\n%s: %.2f\n%s: %.2f",
13 super( first, last, ssn, sales, rate ); 40 "base-salaried commission employee", firstName, lastName,
14 setBaseSalary( salary ); // validate and store base salary 41 "social security number", socialSecurityNumber,
15 } // end six-argument BasePlusCommissionEmployee3 constructor 42 "gross sales", grossSales, "commission rate", commissionRate,
16 43 "base salary", baseSalary );
17 // set base salary 44 } // end method toString
18 public void setBaseSalary( double salary ) 45 } // end class BasePlusCommissionEmployee3
19 {
20 baseSalary = ( salary < 0.0 ) ? 0.0 : salary;
21 } // end method setBaseSalary
22
23 // return base salary
24 public double getBaseSalary()
25 {
26 return baseSalary;
27 } // end method getBaseSalary
28

01/24/2019 DIANA HAIDAR 25


3. CommissionEmployee - BasePlusCommisionEmployee Inheritance Hierarchy using
protected Instance Variables
1 // Fig. 9.11: BasePlusCommissionEmployeeTest3.java 29 employee.setBaseSalary( 1000 ); // set base salary
2 // Testing class BasePlusCommissionEmployee3. 30
3 31 System.out.printf( "\n%s:\n\n%s\n",
4 public class BasePlusCommissionEmployeeTest3
32 "Updated employee information obtained by toString",
5 {
33 employee.toString() );
6 public static void main( String args[] )
34 } // end main
7 {
35 } // end class BasePlusCommissionEmployeeTest3
8 // instantiate BasePlusCommissionEmployee3 object
9 BasePlusCommissionEmployee3 employee = Employee information obtained by get methods:
10 new BasePlusCommissionEmployee3(
First name is Bob
11 "Bob", "Lewis", "333-33-3333", 5000, .04, 300 ); Last name is Lewis
12 Social security number is 333-33-3333
Gross sales is 5000.00
13 // get base-salaried commission employee data Commission rate is 0.04
14 System.out.println( Base salary is 300.00
15 "Employee information obtained by get methods: \n" ); Updated employee information obtained by toString:
16 System.out.printf( "%s %s\n", "First name is",
base-salaried commission employee: Bob Lewis
17 employee.getFirstName() ); social security number: 333-33-3333
18 System.out.printf( "%s %s\n", "Last name is", gross sales: 5000.00
commission rate: 0.04
19 employee.getLastName() ); base salary: 1000.00
20 System.out.printf( "%s %s\n", "Social security number is",
21 employee.getSocialSecurityNumber() );
22 System.out.printf( "%s %.2f\n", "Gross sales is",
23 employee.getGrossSales() );
24 System.out.printf( "%s %.2f\n", "Commission rate is",
25 employee.getCommissionRate() );
26 System.out.printf( "%s %.2f\n", "Base salary is",
27 employee.getBaseSalary() );
28

01/24/2019 DIANA HAIDAR 26


3. CommissionEmployee - BasePlusCommisionEmployee Inheritance Hierarchy using protected
Instance Variables

 Pros.
 Direct access to protected instance variables: A subclass can modify values of the protected instance
variables in the superclass directly.
 Better performance: Avoiding set/get method call overhead.
 Avoiding duplicating the code (the copy-and-paste approach).

 Cons.
 No validity checking: A subclass can assign illegal values, because the validation is implemented in the
set methods in the superclass, but not the subclass.
 Implementation dependent: When changes are required for the superclass, software developers may
need to make changes also in the subclass.

01/24/2019 DIANA HAIDAR 27


Software Engineering with Inheritance
 We can customize the new class to meet our needs by including additional members and by overriding
superclass's source code.
 Java simply requires access to the superclass's .class file so it can compile and execute any program that
uses or extends the superclass.
 This powerful capability is attractive to Independent Software Vendors (ISVs), who can develop proprietary
classes for sale or license and make them available to users in bytecode.
 Users can derive new classes from these library classes rapidly and without accessing the ISV's proprietary
source.

01/24/2019 DIANA HAIDAR 28

You might also like