New version page

UGA CSCI 1301 - chap_08ed5

Documents in this Course
Load more
Upgrade to remove ads

This preview shows page 1-2-3-4-26-27-28-54-55-56-57 out of 57 pages.

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

Upgrade to remove ads
Unformatted text preview:

Polymorphism and InheritanceObjectivesPolymorphism: OutlineClass InterfacesSlide 5Java InterfacesSlide 7Implementing an InterfaceAn Inheritance as a TypeSlide 10Extending an InterfaceInheritance Basics: OutlineInheritance BasicsDerived ClassesSlide 15Overriding Method DefinitionsOverriding Versus OverloadingThe final ModifierPrivate Instance Variables, MethodsUML Inheritance DiagramsSlide 21Programming with Inheritance: OutlineSlide 23Constructors in Derived ClassesThe this Method – AgainCalling an Overridden MethodProgramming ExampleSlide 28Type CompatibilitySlide 30The Class ObjectSlide 32A Better equals MethodCase StudySlide 35Slide 36Slide 37Slide 38Slide 39Abstract ClassesSlide 41Slide 42Dynamic Binding and InheritanceSlide 44Graphics Supplement: OutlineThe Class JAppletThe Class JFrameSlide 48Window Events and Window ListenersThe ActionListener InterfaceSlide 51SummarySlide 53Slide 54Slide 55Slide 56Slide 57JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank Carrano.ISBN 0136130887 © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights ReservedWalter SavitchFrank M. CarranoWalter SavitchFrank M. CarranoPolymorphism and InheritanceChapter 8JAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank Carrano.ISBN 0136130887 © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights ReservedObjectives•Describe polymorphism and inheritance in general•Define interfaces to specify methods•Describe dynamic binding•Define and use derived classes in Java•Use the class Jframe to produce windowing interfaces within Java application programsJAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank Carrano.ISBN 0136130887 © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights ReservedPolymorphism: Outline•Class Interfaces•Java Interfaces•Implementing an Interface•An Interface as a Type•Extending an InterfaceJAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank Carrano.ISBN 0136130887 © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights ReservedClass Interfaces•Consider a set of behaviors for petsBe namedEatRespond to a command•We could specify method headings for these behaviors•These method headings can form a class interfaceJAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank Carrano.ISBN 0136130887 © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights ReservedClass Interfaces•Now consider different classes that implement this interfaceThey will each have the same behaviorsNature of the behaviors will be different•Each of the classes implements the behaviors/methods differentlyJAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank Carrano.ISBN 0136130887 © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights ReservedJava Interfaces•A program component that contains headings for a number of public methodsWill include comments that describe the methods•Interface can also define public named constants•View example interface, listing 8.1interface MeasurableJAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank Carrano.ISBN 0136130887 © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights ReservedJava Interfaces•Interface name begins with uppercase letter•Stored in a file with suffix .java•Interface does not include Declarations of constructorsInstance variablesMethod bodiesJAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank Carrano.ISBN 0136130887 © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights ReservedImplementing an Interface•To implement a method, a class mustInclude the phrase implements Interfac e_nameDefine each specified method•View sample class, listing 8.2class Rectangle implements Measurable•View another class, listing 8.3 which also implements Measurableclass CircleJAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank Carrano.ISBN 0136130887 © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights ReservedAn Inheritance as a Type•Possible to write a method that has a parameter as an interface typeAn interface is a reference type•Program invokes the method passing it an object of any class which implements that interfaceJAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank Carrano.ISBN 0136130887 © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights ReservedAn Inheritance as a Type•The method can substitute one object for anotherCalled polymorphism•This is made possible by mechanism Dynamic bindingAlso known as late bindingJAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank Carrano.ISBN 0136130887 © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights ReservedExtending an Interface•Possible to define a new interface which builds on an existing interfaceIt is said to extend the existing interface•A class that implements the new interface must implement all the methods of both interfacesJAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank Carrano.ISBN 0136130887 © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights ReservedInheritance Basics: Outline•Derived Classes•Overriding Method Definitions•Overriding Versus Overloading•The final Modifier•Private Instance Variables and Private Methods of a Base Class•UML Inheritance DiagramsJAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank Carrano.ISBN 0136130887 © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights ReservedInheritance Basics•Inheritance allows programmer to define a general class•Later you define a more specific classAdds new details to general definition•New class inherits all properties of initial, general class•View example class, listing 8.4class PersonJAVA: An Introduction to Problem Solving & Programming, 5th Ed. By Walter Savitch and Frank Carrano.ISBN 0136130887 © 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights ReservedDerived Classes•Class Person used as a base classAlso called superclass•Now we declare


View Full Document
Download chap_08ed5
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 chap_08ed5 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 chap_08ed5 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?