Docsity

Polymorphism Introduction - Lecture Slides | CMSC 131, Study notes of Computer Science

Toggle sidebar
bg1
1
CMSC 131 Spring 2009
Jan Plane (Adapted from Bonnie Dorr)
Lecture Set #12:
Polymorphism
Introduction
1. Wrappers
2. Interfaces
CMSC 131 Spring 2009
Jan Plane (Adapted from Bonnie Dorr)
1
Wrappers
๎˜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 also contain other useful conversion operations (to / from String, etc.)
๎˜Wrappers included in java.lang:
๎˜Byte
๎˜Short
๎˜Integer
๎˜Long
๎˜Float
๎˜Double
๎˜Character
๎˜Boolean
CMSC 131 Spring 2009
Jan Plane (Adapted from Bonnie Dorr)
2
The 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.
pf3

Partial preview of the text

Download Polymorphism Introduction - Lecture Slides | CMSC 131 and more Study notes Computer Science in PDF only on Docsity!

CMSC 131 Spring 2009 Jan Plane (Adapted from Bonnie Dorr)

Lecture Set #12:

Polymorphism

Introduction

1. Wrappers

2. Interfaces

CMSC 131 Spring 2009 Jan Plane (Adapted from Bonnie Dorr)

1

Wrappers

 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 also contain other useful conversion operations (to / from String, etc.)  Wrappers included in java.lang:  Byte  Short  Integer  Long  Float  Double  Character  Boolean

CMSC 131 Spring 2009 Jan Plane (Adapted from Bonnie Dorr) 2

The 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โ€

 Comparable in Java 5.0 is a interface

 Has compareTo method.

CMSC 131 Spring 2009 Jan Plane (Adapted from Bonnie Dorr)

3

Code 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 code

CMSC 131 Spring 2009 Jan Plane (Adapted from Bonnie Dorr)

4

Polymorphism

 Using an interface we can create one variable that can reference objects of different types (i.e. 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 C provides implementations of all of Iโ€™s abstract methods  A class implementing an interface can also provide other methods or implement other interfaces

CMSC 131 Spring 2009 Jan Plane (Adapted from Bonnie Dorr) 5

In 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 Cats