DOC PREVIEW
UNF COP 2551 - Lecture Notes

This preview shows page 1-2-3-26-27-28 out of 28 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 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 28 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 28 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 28 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 28 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 28 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Chapter 8InheritanceOutlineSlide 4Slide 5Slide 6Deriving SubclassesSlide 8Slide 9Slide 10Slide 11Slide 12The protected ModifierSlide 14Final Class Diagram for WordsThe super ReferenceSlide 17Slide 18Slide 19Slide 20Multiple InheritanceSlide 22Overriding Methods (not Overloading!)Slide 24Slide 25Slide 26OverridingOverloading vs. OverridingChapter 8InheritancePart 1© 2004 Pearson Addison-Wesley. All rights reserved 8-2Inheritance•Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes•Chapter 8 focuses on:deriving new classes from existing classesthe protected modifiercreating class hierarchiesabstract classesindirect visibility of inherited membersdesigning for inheritancethe GUI component class hierarchyextending listener adapter classesthe Timer class© 2004 Pearson Addison-Wesley. All rights reserved 8-3OutlineCreating SubclassesOverriding MethodsClass HierarchiesInheritance and VisibilityDesigning for InheritanceInheritance and GUIsThe Timer Class© 2004 Pearson Addison-Wesley. All rights reserved 8-4Inheritance•Inheritance allows a software developer to derive a new class from an existing one The existing class is called the parent class, or superclass, or base class The derived class is called child class or subclass•As the name implies, the child inherits characteristics of the parent•That is, the child class inherits the methods and data defined by the parent class© 2004 Pearson Addison-Wesley. All rights reserved 8-5Inheritance•Inheritance relationships are shown in a UML class diagram using a solid arrow with an unfilled triangular arrowhead pointing to the parent classVehicleCar Proper inheritance creates an is-a relationship, meaning the child is a more specific version of the parentPlease note: a ‘dashed’arrow with same trianglemeans ‘realizes’ in UML.This is quite different! Thereare not many of these symbols, but you definitely need to learn the ones we see.© 2004 Pearson Addison-Wesley. All rights reserved 8-6Inheritance•A programmer can tailor a derived class as needed by adding new variables or methods, or by modifying the inherited ones•Software reuse is a fundamental benefit of inheritance•By using existing software components to create new ones, we capitalize on all the effort that went into the design, implementation, and testing of the existing software•Component-based software development is HUGE in the industry today!© 2004 Pearson Addison-Wesley. All rights reserved 8-7Deriving Subclasses•In Java, we use the reserved word extends to establish an inheritance relationship•Let’s look at some code…class Car extends Vehicle{ // class contents}base class(parent class)derived class(child class)© 2004 Pearson Addison-Wesley. All rights reserved 8-8Launch Internet Explorer Brows er.lnk//********************************************************************// Words.java Author: Lewis/Loftus//// Demonstrates the use of an inherited method.//********************************************************************public class Words{//-----------------------------------------------------------------// Instantiates a derived class and invokes its inherited and local methods.//----------------------------------------------------------------- public static void main (String[] args) {Dictionary webster = new Dictionary();System.out.println ("Number of pages: " + webster.getPages());System.out.println ("Number of definitions: " + webster.getDefinitions());System.out.println ("Definitions per page: " + webster.computeRatio()); } // end main()} // end Words Looks simple enough. Creating an object webster of type Dictionary.Then we are executing a few of webster’s methods (from the class…)So, we need to see what the Dictionary class looks like, right?© 2004 Pearson Addison-Wesley. All rights reserved 8-9//********************************************************************// Dictionary.java Author: Lewis/Loftus// Represents a dictionary, which is a book. Used to demonstrate// inheritance.//********************************************************************public class Dictionary extends Book  But we note that Dictionary ‘inherits’ Book!{ // Parent (super, or base) class is Book. Dictionary is a derived // class, and our object, webster, is an object of the derived class // Dictionary. private int definitions = 52500; //----------------------------------------------------------------- // Prints a message using both local and inherited values. //----------------------------------------------------------------- public double computeRatio () { return definitions/pages; } // end computeRation() // Definitions mutator. ======================== public void setDefinitions (int numDefinitions) { definitions = numDefinitions; } // end mutator // Definitions accessor. ========================= public int getDefinitions () { return definitions; } // end accessor} // end DictionaryNote methods here inDictionary (usable bywebster, of course.Again, mutator andaccessor methods arequite common.Now, onto the base class!© 2004 Pearson Addison-Wesley. All rights reserved 8-10//********************************************************************// Book.java Author: Lewis/Loftus//// Represents a book. Used as the parent of a derived class to demonstrate inheritance.//********************************************************************public class Book{ protected int pages = 1500; // instance variable for objects. // Pages mutator.public void setPages (int numPages){ pages = numPages;} // end setPages// Pages accessor.public int getPages (){ return pages;} // end getPages()} // end BookStandard mutator andaccessor (get and set methods…)Let’s go back to the main one…© 2004 Pearson Addison-Wesley. All rights reserved 8-11Launch Internet Explorer Brows er.lnk//********************************************************************// Words.java Author: Lewis/Loftus//// Demonstrates the use of an inherited method.//********************************************************************public class Words{//-----------------------------------------------------------------// Instantiates a derived class and invokes its inherited and local methods.//----------------------------------------------------------------- public static void main (String[] args) {Dictionary


View Full Document

UNF COP 2551 - Lecture Notes

Download Lecture Notes
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 Lecture Notes 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 Lecture Notes 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?