Chapter 13 - InheritanceGoalsIntroductionSlide 4Slide 5Superclass / SubclassPowerPoint PresentationInheritance vs InterfaceCode ReuseSlide 10EncapsulationSlide 12Slide 13SyntaxInheritance HierarchySlide 16Slide 17ExampleSlide 19Slide 20Inheriting Instance Fields and MethodsOverriding methodsInheriting methodsAdding methodsInheriting Instance FieldsInheriting FieldsCheckingAccount ClassSlide 28Slide 29Inheriting Private FieldsSlide 31Slide 32Slide 33Invoking a Superclass MethodSlide 35Slide 36Slide 37Example (Continued)Object ClassSlide 40Object Class MethodsInheritance and ConstructorsSlide 43Slide 44Slide 45Slide 46Slide 47Slide 48ConversionsSlide 50Slide 51Slide 52Slide 53Slide 54Super to Sub ConversioninstanceOfConvert from Super to SubPolymorphismSlide 59Slide 60Slide 61Slide 62Slide 63Access ControlSlide 65Recommended Access LevelsRecommend AccessOverride (redefine)Object: The Cosmic SuperclasstoString()Slide 71Slide 72Slide 73Slide 74Slide 75Slide 76Chapter 13 - Chapter 13 - InheritanceInheritanceGoalsGoalsTo learn about inheritance To learn about inheritance To understand how to inherit and override To understand how to inherit and override superclass methods superclass methods To be able to invoke superclass constructors To be able to invoke superclass constructors To learn about protected and package To learn about protected and package access control access control To understand the common superclass To understand the common superclass Object and to override its toString and Object and to override its toString and equals methods equals methodsIntroductionIntroductionIn OOP languages, new classes can In OOP languages, new classes can be derived from an existing class.be derived from an existing class.Why?Why?organizes related classesorganizes related classesreduces code redundancyreduces code redundancyincreases code reuseincreases code reuseenables enables polymorphic polymorphic referencesreferencesIntroductionIntroductionInheritance: extend classes by Inheritance: extend classes by adding methods and fields adding methods and fields Example: Savings account is a bank Example: Savings account is a bank account with interest account with interest class SavingsAccount extends BankAccountclass SavingsAccount extends BankAccount{{new methodsnew methodsnew instance fieldsnew instance fields } }IntroductionIntroductionSavingsAccountSavingsAccount automatically inherits all automatically inherits all methods and instance fields of methods and instance fields of BankAccount BankAccount SavingsAccount collegeFund = new SavingsAccount(10);SavingsAccount collegeFund = new SavingsAccount(10);// Savings account with 10% interest // Savings account with 10% interest collegeFund.deposit(500); collegeFund.deposit(500); // OK to use BankAccount method with SavingsAccount // OK to use BankAccount method with SavingsAccount // object// objectSuperclass / SubclassSuperclass / SubclassOriginal/base class is known as the Original/base class is known as the superclasssuperclass ( (BankAccountBankAccount))extending class is the extending class is the subclasssubclass ((SavingsAccountSavingsAccount) )Every class Every class extends the extends the ObjectObject class either class either directly or directly or indirectly indirectlyInheritance vs InterfaceInheritance vs InterfaceInheriting from an existing class IS Inheriting from an existing class IS NOT the same as implementing NOT the same as implementing interfaceinterfaceA subclass inherits behavior and stateA subclass inherits behavior and stateInterfaces have no state or defined Interfaces have no state or defined behaviorbehaviorCode ReuseCode ReuseOne advantage of inheritance is code One advantage of inheritance is code reuse reuse Not “reinventing the wheel”Not “reinventing the wheel”Already have a class that does some Already have a class that does some base functions, why not just build up on base functions, why not just build up on ititdepositdeposit, , withdrawwithdraw, , getBalancegetBalance common common among all accountsamong all accountsIn subclass, specify added instance fields, In subclass, specify added instance fields, added methods, and changed or overridden added methods, and changed or overridden methods methods Inheritance takes care of what is common, you Inheritance takes care of what is common, you define what is differentdefine what is differentpublic class SavingsAccount extends BankAccount{public class SavingsAccount extends BankAccount{private double interestRate; private double interestRate; public SavingsAccount(double rate) {public SavingsAccount(double rate) {interestRate = rate; interestRate = rate; }}public void addInterest() { public void addInterest() { double interest = getBalance() * interestRate / double interest = getBalance() * interestRate / 100; 100; deposit(interest); deposit(interest); }}}}EncapsulationEncapsulationWhy do we call Why do we call getBalancegetBalance? Why not ? Why not just use just use balancebalance??double interest = getBalance() * double interest = getBalance() * interestRate / 100;interestRate / 100;Encapsulation: Encapsulation: addInterest addInterest calls calls getBalancegetBalance because because balancebalance field of the field of the superclass is superclass is privateprivateCannot access private members of Cannot access private members of another classanother classEncapsulationEncapsulationSavingsAccountSavingsAccount object inherits the object inherits the balance balance instance field from instance field from BankAccountBankAccount, and , and gains one additional instance field:gains one additional instance field: interestRateinterestRateEncapsulationEncapsulationNote that Note that addInterestaddInterest calls calls getBalancegetBalance without specifying an implicit without specifying an implicit parameter (the calls apply to the parameter (the calls apply to the same object) same object) Means the call to is Means the call to is getBalancegetBalance is is applied to the same object as the applied to the same object as the object that called object that called addInterestaddInterestSyntaxSyntaxclass class SubclassNameSubclassName extends extends SuperclassNameSuperclassName{ { extra extra instance fields instance fields extra methods extra methods } }Inheritance HierarchyInheritance HierarchyInheritance is a way to Inheritance is a way to
View Full Document