DOC PREVIEW
UMD CMSC 131 - Lecture Set #11: Polymorphism Introduction

This preview shows page 1 out of 4 pages.

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

Unformatted text preview:

1CMSC 131 Spring 2010Jan Plane & Ben BedersonLecture Set #11:Polymorphism Introduction1. Wrappers2. InterfacesCMSC 131 Fall 2009Jan Plane (Adapted from Bonnie Dorr)1Wrappers We may want to treat primitives as though they were objects For example, generic routines can be implemented using interfaces … but they are not usable on primitive types To overcome this problem, Java provides wrappers for primitive types Wrappers: classes whose objects contain single values of the “wrapped type” Wrappers convert easily to and from that “wrapped type” Wrappers also contain other useful conversion operations (to / from String, etc.) Wrappers included in java.lang: Byte Short Integer Long Float Double Character Boolean2CMSC 131 Fall 2009Jan Plane (Adapted from Bonnie Dorr)2The Integer Wrapper The documentation is on-line at http://java.sun.com/j2se/1.5.0/docs/api/ Notes Immutable  Constructors Implements Comparable Documentation says “Comparable<Integer>” Comparable in Java 5.0 is a interface Has compareTo method.CMSC 131 Fall 2009Jan Plane (Adapted from Bonnie Dorr)3Code Re-use Many operations recur in programming sorting max / min(These operations may apply to strings, numbers, etc.) Desirable: one implementation! Less coding Less likely to have typos Easier maintenance of code3CMSC 131 Fall 2009Jan Plane (Adapted from Bonnie Dorr)4Polymorphism Using an interface we can create one variable that can reference objects different types (i.e. Comparable variable referencing Integers, Strings or Cats; UMStudent variable referencing CSMajor, CEMajor or PsychMajor) This form of “generalization” is called polymorphism Hallmark of OO languages Allows application of same code to objects of different types Polymorphism: “A variable that takes on many shapes.” Interfaces: one mechanism Java provides for polymorphism a collection of prototypes (method prototypes but no bodies) aka abstract methods A class C implements an interface I  If and only if C provides implementations of all of I’s abstract methods  A class implementing an interface can also provide other methods or implement other interfacesCMSC 131 Fall 2009Jan Plane (Adapted from Bonnie Dorr)5In class Demo: Implementing a method using the Integer class Create objects of type Integer  using the constructor can be based on int type values or variables Create an array of Integer type object references and those objects of type Integer Use the API to access information about the data in the Integer class Expand this example to Strings Expand this example to Cats4CMSC 131 Fall 2009Jan Plane (Adapted from Bonnie Dorr)6Adapting Cat to Implement Comparable The Comparable Interface  insists that I must implement compareTo method which has the following prototype:int compareTo(Object o) it must return a negative if the current object is less, a positive if the current object is greater or a 0 if they are the same. What is Object? Type of all possible objects in any class Shortcoming of (earlier) Java: no good way to say “same type as this” Instead: Implementation must take any objectCMSC 131 Fall 2009Jan Plane (Adapted from Bonnie Dorr)7What about int? char? Polymorphic findMin can be used on any class implementing Comparable What about primitive types (int, char, double, etc.)? They are not classes So they do not implement Comparable Hence findMin cannot be used on them That’s why we use


View Full Document

UMD CMSC 131 - Lecture Set #11: Polymorphism Introduction

Documents in this Course
Set #3

Set #3

7 pages

Exam #1

Exam #1

6 pages

Exam #1

Exam #1

6 pages

Notes

Notes

124 pages

Notes

Notes

124 pages

Load more
Download Lecture Set #11: Polymorphism Introduction
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 Set #11: Polymorphism Introduction 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 Set #11: Polymorphism Introduction 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?