Object Oriented Programming: InheritanceWhat You Will LearnIntroductionSlide 4Base Classes & Derived ClassesSuperclass and SubclassDesign TipDeclaring Classes Using InheritanceComments on Private vs. ProtectedThree Level Inheritance HierarchyConstructors in SubclassesFinalizers in SubclassesSoftware Engineering with InheritanceObject Oriented Object Oriented Programming: InheritanceProgramming: InheritanceChapter 9Chapter 922What You Will LearnWhat You Will LearnSoftware reusabilitySoftware reusability(Recycling)(Recycling)Inheriting data members and methods from Inheriting data members and methods from previously defined classespreviously defined classes33IntroductionIntroductionSoftware ReusabilitySoftware Reusabilitysaves time in program developmentsaves time in program developmentencourages use of proven, debugged codeencourages use of proven, debugged codereduces problemsreduces problemsWrite programs in general fashionWrite programs in general fashionEnables software designers to deal with Enables software designers to deal with complexity of modern softwarecomplexity of modern software44IntroductionIntroductionWhen creating a new class …When creating a new class …designate that class to inherit data members, designate that class to inherit data members, functions of previously defined functions of previously defined superclasssuperclassresult is a result is a subclasssubclassClass can be derived from one or multiple Class can be derived from one or multiple classesclassesSubclass adds new data members and Subclass adds new data members and functionsfunctionsReplace and refine existing membersReplace and refine existing members55Base Classes & Derived Base Classes & Derived ClassesClassesSuperclass is more generalSuperclass is more generalstudent, shape, loanstudent, shape, loanSubclass is more specificSubclass is more specificgrad student, undergradgrad student, undergradcircle, triangle, rectanglecircle, triangle, rectanglecarloan, home improvement, mortgagecarloan, home improvement, mortgageSome languages talk ofSome languages talk ofBase class (Superclass)Base class (Superclass)Derived class (Subclass)Derived class (Subclass)66Superclass and SubclassSuperclass and SubclassInheritance produces tree like structuresInheritance produces tree like structuresB a n k i n g H i e r a r c h yS u p e r - N o w A c c o u n tD e r i v e d C l a s sC h e c k i n g A c c o u n tD e r i v e d C l a s sS a v i n g s A c c o u n tD e r i v e d C l a s sB a n k A c c o u n tB a s e C l a s s- Checking & Savi ngs are derived from Bank Account Class- Super-Now class derived from Checking class- Checking & Savi ngs are derived from Bank Account Class- Super-Now class derived from Checking class77Design TipDesign TipImportant link between subclass and Important link between subclass and superclasssuperclassThe “IS-A” relationshipThe “IS-A” relationshipExamplesExamplesA checking account IS-A banking accountA checking account IS-A banking accountA savings account IS NOT a checking accountA savings account IS NOT a checking accountIf there is no IS-A relationship, do not If there is no IS-A relationship, do not use inheritanceuse inheritance88Declaring Classes Using Declaring Classes Using InheritanceInheritanceView superclass Point.java, View superclass Point.java, Figure 9.9Figure 9.9View subclass Circle.java, View subclass Circle.java, Figure 9.10Figure 9.10NoteNoteThe The proteted proteted specification for the superclass specification for the superclass datadataThe The extendsextends specification specificationThe implicit call to the superclass constructorThe implicit call to the superclass constructorThe explicit call with super( … )The explicit call with super( … )View the test program, View the test program, Figure 9.11 Figure 9.1199Comments on Private vs. ProtectedComments on Private vs. ProtectedUse Use protectedprotected when when Superclass should provide a service only to its Superclass should provide a service only to its subclassessubclassesShould not provide service to other clientsShould not provide service to other clientsUse Use privateprivate so that so thatSuperclass implementation can change without affecting Superclass implementation can change without affecting subclass implementationssubclass implementationsAuthor advocates avoiding Author advocates avoiding protectedprotectedInstead provide set and get methods to access Instead provide set and get methods to access privateprivate data items (see Figures 9.12, 9.13 in text)data items (see Figures 9.12, 9.13 in text)1010Three Level Inheritance HierarchyThree Level Inheritance HierarchySo far we have superclass Point and So far we have superclass Point and subclass Circlesubclass CircleConsider creating a subclass Cylinder, Consider creating a subclass Cylinder, derived from Circlederived from CircleView class Cylinder, Figure 9.15View class Cylinder, Figure 9.15Note the program for testing class Cylinder, Note the program for testing class Cylinder, Figure 9.16Figure 9.161111Constructors in SubclassesConstructors in SubclassesWhen you instantiate a subclass objectWhen you instantiate a subclass objectSubclass constructor invokes superclass Subclass constructor invokes superclass constructorconstructorImplicitlyImplicitlyExplicitly with super ( ) referenceExplicitly with super ( ) referenceSubclass constructor performs its own tasksSubclass constructor performs its own tasksSuperclass constructor may also invoke a Superclass constructor may also invoke a constructor in the next class up the hierarchyconstructor in the next class up the hierarchyLast constructor in the chain is always the Last constructor in the chain is always the constructor for constructor for ObjectObject1212Finalizers in SubclassesFinalizers in SubclassesWhen classes in your hierarchy declare their When classes in your hierarchy declare their own own finalizefinalize methods … methods …A subclass A subclass finalizefinalize should invoke the should invoke the superclass superclass finalizefinalize as its last action as its last actionEnsures all parts of an object are finalized Ensures all parts of an object are finalized properlyproperlyMarked correctly for garbage collectionMarked correctly for garbage collectionNote
View Full Document