Object Oriented Programming: Inheritance Chapter 9
2 Introduction Software Reusability –saves __________ in program development –encourages use of proven, ____________ code –reduces problems Write programs in ____________ fashion Enables software designers to deal with complexity of modern software
3 Introduction When creating a new class … –designate that class to ___________ data members, functions of previously defined superclass –result is a ___________________ Subclass ____________ new data members and functions ________________ and refine existing members
4 Base Classes & Derived Classes Superclass is more ______________ –student, shape, loan ________________ is more specific –grad student, undergrad –circle, triangle, rectangle –carloan, home improvement, mortgage Some languages talk of –______________ class (Superclass) –Derived class (Subclass)
5 Superclass and Subclass Inheritance produces _________ like structures
6 Superclass and Subclass Inheritance produces tree like structures
7 Design Tip Important link between subclass and superclass –The “_____________” relationship Examples –A checking account IS-A banking account –A savings account IS NOT a checking account If there is no IS-A relationship, do not use __________________
8 protected Members protected access –______________ level of protection between public and private –protected members accessible by superclass members _________________ members Class members in the same package –Subclass access to superclass member Keyword _____________ and a dot (.)
9 Comments on Private vs. Protected Use protected when –Superclass should provide a _____________ only to its subclasses –Should not provide service to other clients Use private so that –________________ implementation can change without affecting subclass implementations Author advocates ______________ protected –Instead provide set and get methods to access private data items (see Figures 9.12, 9.13 in text)Figures
10 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 –First name, last name, SSN, commission rate, gross sale amount –Base salary
11 Creating and Using a CommissionEmployee Class Class CommissionEmployee –Extends class Object ______________________ extends Every class in Java extends an existing class –Except Object Every class inherits __________________ methods New class _________________ extends Object –If it does not extend another class
12 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 View class Figure 9.4Figure 9.4 View test program, Figure 9.5Figure 9.5
13 Creating a BasePlusCommissionEmployee Class without Using Inheritance Class BasePlusCommissionEmployee –Implicitly extends Object –Much of the code is similar to C ommissionEmployee private ____________ variables public methods constructor –____________ private instance variable baseSalary Methods setBaseSalary and getBaseSalary
14 Creating a CommissionEmployee- BasePlusCommiionEmployee Inheritance Hierarchy Class BasePlusCommissionEmployee2 –Extends class CommissionEmployee –Is a CommissionEmployee –Has ___________ variable baseSalary –_____________ public and protected members –_______________ not inherited Note these points plus invalid references, Figure 9.8 – note compiler error messagesFigure 9.8 error messages
15 Using protected Instance Variables Use protected instance variables –Enable class BasePlusCommissionEmployee to __________________ superclass instance variables –Superclass’s protected members are inherited by all subclases of that superclass View version that now works, Figure 9.10Figure 9.10 Test program, Figure 9.11Figure 9.11
16 Using protected Instance Variables Advantages Subclasses can _________________ values directly Slight __________________ in performance –Avoid set/get method call overhead
17 Using protected Instance Variables Disadvantages No ________________ checking –subclass can assign illegal value Implementation dependent –subclass methods more likely __________ on superclass implementation –superclass implementation changes may result in subclass ___________________ Fragile (brittle) software
18 Reexamine Hierarchy Use the best software engineering practice –Declare instance variables as private –Provide public _____________________ methods –Use _____________ method to obtain values of instance variables View new version of CommissionEmployee, Figure 9.12 Figure 9.12 New version of BasePlusCommissiionEmployee, Figure 9.13Figure 9.13 Test program, Figure 9.14Figure 9.14
19 Instantiating Subclass Object Chain Of Constructor Calls Subclass constructor invokes superclass constructor –__________ or _________________ Base of inheritance hierarchy –Last constructor called in chain is _________________ constructor –_______________ subclass constructor’s body finishes executing last
20 Instantiating Subclass Object Chain Of Constructor Calls Example, Figure 9.15 : CommissionEmployee3- BasePlusCommissionEmployee4 hierarchyFigure 9.15 –CommissionEmployee3 constructor called second last (last is Object constructor) –CommissionEmployee3 constructor’s body finishes execution second (first is Object constructor’s body) Figure 9.16, BasePlusCommissionEmploye5Figure 9.16 Figure 9.17, Test program, demonstrates constructor callsFigure 9.17
21 Software Engineering with Inheritance At the design stage, certain classes found to be closely related –_______________ common attributes, behaviors –Place these in a ____________________ –Use inheritance to develop subclasses with inherited capabilities This avoids _____________ of classes, "reinventing the wheel" Note –Declaring a subclass ______________________ superclass source code