CSC245 Lecture 3
CSC245 Lecture 3
Programming: Inheritance
Superclass Subclasses
Student GraduateStudent, UndergraduateStudent
Shape Circle, Triangle, Rectangle
Loan CarLoan, HomeImprovementLoan,
MortgageLoan
Employee Faculty, Staff
BankAccount CheckingAccount, SavingsAccount
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;
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.