Object Oriented Programming: InheritanceWhat You Will LearnIntroductionSlide 4Base Classes & Derived ClassesSuperclass and SubclassSlide 7Design Tipprotected MembersComments on Private vs. ProtectedRelationship between Superclasses and SubclassesCreating and Using a CommissionEmployee ClassSlide 13Creating a BasePlusCommissionEmployee Class without Using InheritanceCreating a CommissionEmployee-BasePlusCommiionEmployee Inheritance HierarchyUsing protected Instance VariablesUsing protected Instance Variables AdvantagesUsing protected Instance Variables DisadvantagesReexamine HierarchyInstantiating Subclass ObjectSlide 21Software 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 subclasssubclassSubclass 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 structures77Superclass and SubclassSuperclass and SubclassInheritance produces tree like structuresInheritance produces tree like structures88Design 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 inheritanceSun comments on the concept of inheritanceSun comments on the concept of inheritance99protectedprotected Members Membersprotectedprotected access accessIntermediate level of protection between Intermediate level of protection between publicpublic and and privateprivateprotectedprotected members accessible by members accessible bysuperclass memberssuperclass memberssubclass memberssubclass membersClass members in the same packageClass members in the same packageSubclass access to superclass memberSubclass access to superclass memberKeyword Keyword supersuper and a dot (.) and a dot (.)1010Comments 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 data items (see Figures 9.12Figures 9.12, , 9.139.13 in text) in text)1111Relationship between Superclasses Relationship between Superclasses and Subclassesand SubclassesSuperclass and subclass relationshipSuperclass and subclass relationshipExample: Example: CommissionEmployee/CommissionEmployee/BasePlusCommissionEmployeeBasePlusCommissionEmployee inheritance inheritance hierarchyhierarchyCommissionEmployeeCommissionEmployeeFirst name, last name, SSN, commission rate, gross sale First name, last name, SSN, commission rate, gross sale amountamountBasePlusCommissionEmployeeBasePlusCommissionEmployeeFirst name, last name, SSN, commission rate, gross sale First name, last name, SSN, commission rate, gross sale amountamountBase salaryBase salary1212Creating and Using a Creating and Using a CommissionEmployeeCommissionEmployee Class ClassClass Class CommissionEmployeeCommissionEmployeeExtends class Extends class ObjectObjectKeyword Keyword extendsextendsEvery class in Java extends an existing classEvery class in Java extends an existing classExcept Except ObjectObjectEvery class inherits Every class inherits ObjectObject’s methods’s methodsNew class implicitly extends ObjectNew class implicitly extends ObjectIf it does not extend another classIf it does not extend another class1313Creating and Using a Creating and Using a CommissionEmployeeCommissionEmployee Class ClassClass Class CommissionEmployeeCommissionEmployeeExtends class Extends class ObjectObjectKeyword Keyword extendsextendsEvery class in Java extends an existing classEvery class in Java extends an existing classExcept Except ObjectObjectEvery class inherits Every class inherits ObjectObject’s methods’s methodsNew class implicitly extends ObjectNew class implicitly extends ObjectIf it does not extend another classIf it does not extend another classView class View class
View Full Document