Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Oriented Programming: Inheritance Chapter 9.

Similar presentations


Presentation on theme: "Object Oriented Programming: Inheritance Chapter 9."— Presentation transcript:

1 Object Oriented Programming: Inheritance Chapter 9

2 2 What You Will Learn  Software reusability (Recycling)  Inheriting data members and methods from previously defined classes

3 3 Introduction  Software Reusability  saves time in program development  encourages use of proven, debugged code  reduces problems  Write programs in general fashion  Enables software designers to deal with complexity of modern software

4 4 Introduction  When creating a new class …  designate that class to inherit data members, functions of previously defined superclass  result is a subclass  Class can be derived from one or multiple classes  Subclass adds new data members and functions  Replace and refine existing members

5 5 Base Classes & Derived Classes  Superclass is more general  student, shape, loan  Subclass is more specific  grad student, undergrad  circle, triangle, rectangle  carloan, home improvement, mortgage  Some languages talk of  Base class (Superclass)  Derived class (Subclass)

6 6 Superclass and Subclass  Inheritance produces tree like structures - Checking & Savings are derived from Bank Account Class - Super-Now class derived from Checking class - Checking & Savings are derived from Bank Account Class - Super-Now class derived from Checking class

7 7 Design Tip  Important link between subclass and superclass  The “IS-A” 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 inheritance

8 8 Declaring Classes Using Inheritance  View superclass Point.java, Figure 9.9 Figure 9.9Figure 9.9  View subclass Circle.java, Figure 9.10 Figure 9.10Figure 9.10  Note  The proteted specification for the superclass data  The extends specification  The implicit call to the superclass constructor  The explicit call with super( … )  View the test program, Figure 9.11 Figure 9.11Figure 9.11

9 9 Comments on Private vs. Protected  Use protected when  Superclass should provide a service only to its subclasses  Should not provide service to other clients  Use private so that  Superclass implementation can change without affecting subclass implementations  Author advocates avoiding protected  Instead provide set and get methods to access private data items (see Figures 9.12, 9.13 in text)

10 10 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 Figure 9.15Figure 9.15  Note the program for testing class Cylinder, Figure 9.16 Figure 9.16 Figure 9.16

11 11 Constructors in Subclasses  When you instantiate a subclass object  Subclass constructor invokes superclass constructor  Implicitly  Explicitly with super ( ) reference  Subclass constructor performs its own tasks  Superclass constructor may also invoke a constructor in the next class up the hierarchy  Last constructor in the chain is always the constructor for Object

12 12 Finalizers in Subclasses When classes in your hierarchy declare their own finalize methods …  A subclass finalize should invoke the superclass finalize as its last action  Ensures all parts of an object are finalized properly  Marked correctly for garbage collection  Note example of finalize functions in Figures 17, 18, and the test Figure 9.19 1718Figure 9.19 1718Figure 9.19

13 13 Software Engineering with Inheritance  At the design stage, certain classes found to be closely related  Factor out common attributes, behaviors  Place these in a superclass  Use inheritance to develop subclasses with inherited capabilities  This avoids proliferation of classes, "reinventing the wheel"  Note  Declaring a subclass does not affect superclass source code


Download ppt "Object Oriented Programming: Inheritance Chapter 9."

Similar presentations


Ads by Google