DOC PREVIEW
LETU COSC 2103 - Object Oriented Programming: Inheritance

This preview shows page 1-2-3-4 out of 13 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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 LearnSoftware reusabilitySoftware reusability(Recycling)(Recycling)Inheriting data members and methods from Inheriting data members and methods from previously defined classespreviously defined classes33IntroductionIntroductionSoftware ReusabilitySoftware Reusabilitysaves time in program developmentsaves time in program developmentencourages use of proven, debugged codeencourages use of proven, debugged codereduces problemsreduces problemsWrite programs in general fashionWrite programs in general fashionEnables software designers to deal with Enables software designers to deal with complexity of modern softwarecomplexity of modern software44IntroductionIntroductionWhen 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 superclasssuperclassresult is a result is a subclasssubclassClass can be derived from one or multiple Class can be derived from one or multiple classesclassesSubclass adds new data members and Subclass adds new data members and functionsfunctionsReplace and refine existing membersReplace and refine existing members55Base Classes & Derived Base Classes & Derived ClassesClassesSuperclass is more generalSuperclass is more generalstudent, shape, loanstudent, shape, loanSubclass is more specificSubclass is more specificgrad student, undergradgrad student, undergradcircle, triangle, rectanglecircle, triangle, rectanglecarloan, home improvement, mortgagecarloan, home improvement, mortgageSome languages talk ofSome languages talk ofBase class (Superclass)Base class (Superclass)Derived class (Subclass)Derived class (Subclass)66Superclass and SubclassSuperclass and SubclassInheritance 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 TipImportant link between subclass and Important link between subclass and superclasssuperclassThe “IS-A” relationshipThe “IS-A” relationshipExamplesExamplesA checking account IS-A banking accountA checking account IS-A banking accountA savings account IS NOT a checking accountA savings account IS NOT a checking accountIf 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 InheritanceInheritanceView superclass Point.java, View superclass Point.java, Figure 9.9Figure 9.9View subclass Circle.java, View subclass Circle.java, Figure 9.10Figure 9.10NoteNoteThe The proteted proteted specification for the superclass specification for the superclass datadataThe The extendsextends specification specificationThe implicit call to the superclass constructorThe implicit call to the superclass constructorThe 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. ProtectedUse Use protectedprotected when when Superclass should provide a service only to its Superclass should provide a service only to its subclassessubclassesShould not provide service to other clientsShould not provide service to other clientsUse Use privateprivate so that so thatSuperclass implementation can change without affecting Superclass implementation can change without affecting subclass implementationssubclass implementationsAuthor advocates avoiding Author advocates avoiding protectedprotectedInstead 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 HierarchySo far we have superclass Point and So far we have superclass Point and subclass Circlesubclass CircleConsider creating a subclass Cylinder, Consider creating a subclass Cylinder, derived from Circlederived from CircleView class Cylinder, Figure 9.15View class Cylinder, Figure 9.15Note the program for testing class Cylinder, Note the program for testing class Cylinder, Figure 9.16Figure 9.161111Constructors in SubclassesConstructors in SubclassesWhen you instantiate a subclass objectWhen you instantiate a subclass objectSubclass constructor invokes superclass Subclass constructor invokes superclass constructorconstructorImplicitlyImplicitlyExplicitly with super ( ) referenceExplicitly with super ( ) referenceSubclass constructor performs its own tasksSubclass constructor performs its own tasksSuperclass 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 hierarchyLast 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 actionEnsures all parts of an object are finalized Ensures all parts of an object are finalized properlyproperlyMarked correctly for garbage collectionMarked correctly for garbage collectionNote


View Full Document

LETU COSC 2103 - Object Oriented Programming: Inheritance

Documents in this Course
Arrays

Arrays

16 pages

Templates

Templates

17 pages

Methods

Methods

22 pages

Methods

Methods

22 pages

Arrays

Arrays

11 pages

Load more
Download Object Oriented Programming: Inheritance
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Object Oriented Programming: Inheritance and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Object Oriented Programming: Inheritance 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?