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 petsBe namedEatRespond 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 interfaceThey will each have the same behaviorsNature 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 methodsWill 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 constructorsInstance variablesMethod 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 mustInclude the phrase implements Interfac e_nameDefine 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 typeAn 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 anotherCalled polymorphism•This is made possible by mechanism Dynamic bindingAlso 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 interfaceIt 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 classAdds 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 classAlso called superclass•Now we declare
View Full Document