Chapter9 Notes - 1 Polymorphism Polymorphism is an...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 Polymorphism Polymorphism is an object-oriented concept that allows us to create versatile software designs Chapter 9 focuses on defining polymorphism and its benefits using inheritance to create polymorphic references using interfaces to create polymorphic references Outline Polymorphic References Polymorphism via Inheritance Interfaces Polymorphism via Interfaces 2 9.1 Binding Consider the following method invocation: obj.doIt(); At some point, this invocation is bound to the definition of the method that it invokes If this binding occurred at compile time, then that line of code would call the same method every time However, Java defers method binding until run time this is called dynamic binding or late binding Late binding provides flexibility in program design 9.1 Polymorphism The term polymorphism literally means having many forms A polymorphic reference is a variable that can refer to different types of objects at different points in time The method invoked through a polymorphic reference can change from one invocation to the next All object references in Java are potentially polymorphic 3 9.1 Polymorphism Suppose we create the following reference variable Occupation job; Java allows this reference to point to an Occupation object, or to any object of any compatible type This compatibility can be established using inheritance or using interfaces Careful use of polymorphic references can lead to elegant, robust software designs Outline Polymorphic References Polymorphism via Inheritance Interfaces Polymorphism via Interfaces 4 7 9.2 References and Inheritance An object reference can refer to an object of its class, or to an object of any class related to it by inheritance For example, if the Holiday class is used to derive a class called Christmas , then a Holiday reference could be used to point to a Christmas object Mammal pet; Horse barbaro = new Horse(); pet = barbaro; Mammal Horse 8 9.2 References and Inheritance Assigning a child object to a parent reference is considered to be a widening conversion, and can be performed by simple assignment Assigning an parent object to a child reference can be done also, but it is considered a narrowing conversion and must be done with a cast The widening conversion is the most useful 5 9.2 Polymorphism via Inheritance It is the type of the object being referenced, not the reference type, that determines which method is invoked Suppose the Mammal class has a method called move , and the Horse class overrides it Now consider the following invocation pet.move(); If pet refers to a Mammal object, it invokes the Mammal version of move ; if it refers to a Horse object, it invokes the Horse version 9.2 Polymorphism via Inheritance Now lets look at an example that pays a set of diverse employees using a polymorphic method 6 9.2 Firm.java //********************************************************************...
View Full Document

This note was uploaded on 03/31/2008 for the course IST 240 taught by Professor Tracymullen during the Spring '08 term at Pennsylvania State University, University Park.

Page1 / 25

Chapter9 Notes - 1 Polymorphism Polymorphism is an...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online