1 / 14

Polymorphism Read Day 6 in OOP21

Polymorphism Read Day 6 in OOP21. C Sc 335 University of Arizona. From Wepodia http://www.webopedia.com/TERM/p/polymorphism.html.

zia-chang
Télécharger la présentation

Polymorphism Read Day 6 in OOP21

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PolymorphismRead Day 6 in OOP21 C Sc 335 University of Arizona

  2. From Wepodia http://www.webopedia.com/TERM/p/polymorphism.html polymorphism: Generally, the ability to appear in many forms. In object-oriented programming, polymorphism refers to a programming language's ability to process objects differently depending on their data type or class. More specifically, it is the ability to redefine methods for derived classes. For example, given a base class shape, polymorphism enables the programmer to define different circumference methods for any number of derived classes, such as circles, rectangles and triangles. No matter what shape an object is, applying the circumference method to it will return the correct results. Polymorphism is considered to be a requirement of any true object-oriented programming language (OOPL).

  3. From Computing Fundamentals, Mercer02 To understand polymorphism, take an example of a typical workday in an office.Someone brought in pastries and everyone stood around chatting. When the food was mostly devoured, Jim, the president of the company, invited everyone to “Get back to work.” Sue went back to read a new section of a book she was editing. Tom continued laying out the new edition of a book. Stephanie went back to figure out some setting in her word-processing program. Ian finished the company catalog. Jeni met with Jim to discuss a new project. Chris began contacting professors to review a new manuscript. And Krista continued her Web search on whether colleges are using C++ or Java. Rick went back to work on the index of his new book. Eight different behaviors from the same message! The message “Get back to work” is a polymorphic message—a message that is understood by many objects (or employees), but responded to with different behaviors by different objects (or employees).

  4. How to get polymorphism • Polymorphism: A single name can represent different behaviors • withdraw deposit actionPerformed toString • In Java, polymorphism is possible through • inheritance • Same message to Account results in 1 of 3 methods • interfaces • JButton and JTextField objects send actionPerformed messages to the listeners

  5. actionPerformeda polymorphic message • Run • Polymorphic_actionPerformed,java • ThreeListeners.java • Four types of polymorphism • Inclusion • Parametric (can use templates) • Overloading • Overriding

  6. PolymorphismInclusion • Allow for a substitutability relationship • Can assign any subclass of Account to a reference variable of type Account Account bAccount = new BankAccount(...); Account fAccount = new FeeAccount(...); Account sAccount = new SafeAccount(...); • Can write one method for different types • Can treat different types of objects generically • Sort any List • Send withdraw messages to any Account

  7. ParametersInclusion Polymorphism • Can pass any subclass argument to a superclass parameter add(Account anySubclass) add(new BankAccount(...)) add(new FeeAccount(...); add(new SafeAccount(...));

  8. Parameters • Can pass instances of a class that implement an interface to that interface type parameter addActionListener(ActionListener anyListener) addActionListener(new ButtonListener()); addActionListener(new TextFieldListener()); • Both classes must implement the ActionListener interface • This design prefers compile time errors rather than the runtime errors that occur later when the button sends a message the object does not understand

  9. Parametric Polymorphism • Generic methods and Generic Types • Templates in C++ since 97, may come to Java • Construct a generic vector using templates vector<int> testScore(100, -1); // From Computing Fundamentals with C++, Mercer 94 template<class VectorElementType> class vector { public: vector(int initCapacity, VectorElementType initVal); vector(const vector<VectorElementType> & source); ~vector(); void resize(int newCapacity);

  10. Overloadingad-hoc polymorphism • a.k.a. ad-hoc polymoprhism • same method names • different number and/or type of arguments • Three constructors (Java does this often) • vector<int> tests; • vector<double> x(100); • vector<string> names (100, "Default") template<class VectorElementType> class vector { public: vector(); // default capacity and values vector(int initCapacity); // use the types default values vector(int initCapacity, VectorElementType initVal);

  11. Overloading • Some languages allow operator overloading // Allow x[1] = 0.0; VectorElementType& operator [] (int subscript);

  12. Method overloading in Java • Constructors in the PrintWriter class PrintWriter(OutputStream out) PrintWriter(OutputStream out, boolean autoFlush) PrintWriter(Writer out) PrintWriter(Writer out, boolean autoFlush) • Methods in the PrintWriter class void print(boolean b) void print(char c) void print(char[] s) void print(double d) void print(float f) void print(int i) void print(long l) void print(Object obj) void print(String s)

  13. Overriding • Subclasses give new behavior to methods • Famous override: toString • Can specialize by doing stuff and still calling the superclass method

  14. Coercion • Makes an int look like a double add(double x, double y); add( 1, 2);

More Related