Download presentation
Presentation is loading. Please wait.
1
Object Oriented Programming: Inheritance
Chapter 9
2
Introduction Software Reusability Write programs in __________ fashion
saves __________ in program development encourages use of proven, debugged code ______________ 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 adds new __________ and ____________ Replace 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 Banking Hierarchy Super-Now Account Derived Class Checking Account Savings Account Bank Account Base Class - Checking & Savings are derived from Bank Account Class - Super-Now class derived from Checking class
6
Design Tip Important link between subclass and superclass Examples
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 ______________
7
Declaring Classes Using Inheritance
View superclass Point.java, Figure 9.9 View subclass Circle.java, Figure 9.10 Note The _____________ specification for the superclass data The ____________ specification The implicit call to the superclass constructor The explicit call with super( … ) View the test program, Figure 9.11
8
Comments on Private vs. Protected
Use protected when Superclass should ___________________ only to its subclasses Should not provide service to __________ Use private so that Superclass ______________ 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)
9
Three Level Inheritance Hierarchy
So far we have superclass Point and subclass Circle Consider creating a subclass Cylinder, derived from Circle View class Cylinder, Figure 9.15 Note the program for testing class Cylinder, Figure 9.16
10
Constructors in Subclasses
When you instantiate a subclass object Subclass constructor invokes ____________ constructor Implicitly Explicitly with _____________ reference ____________ constructor performs its own tasks Superclass constructor may also invoke a ___________ in the next class up the hierarchy Last constructor in the chain is always the constructor for ______________
11
Finalizers in Subclasses
When classes in your hierarchy declare their own finalize methods … A _____________ finalize should invoke the superclass finalize as its ____________ Ensures all parts of an object are finalized properly Marked correctly for __________________ Note example of finalize functions in Figures 17, 18, and the test Figure 9.19
12
Software Engineering with Inheritance
At the design stage, certain classes found to be closely related Factor out ____________ attributes, behaviors Place these in a ________________ Use _______________ to develop subclasses with inherited capabilities This avoids proliferation of classes, "reinventing the wheel" Note Declaring a subclass does not affect superclass ___________________
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.