Java Polymorphism: Wrappers and Interfaces in CMSC 131 Fall 2008 - Prof. Bonnie J. Dorr, Study notes of Computer Science

This document from the cmsc 131 fall 2008 course covers the concepts of polymorphism, wrappers, and interfaces in java. It explains how java provides wrappers for primitive types to enable the use of generic routines and interfaces. The document also discusses the importance of code re-use and polymorphism in object-oriented programming. An example is given of implementing a method using the integer class and adapting cat to implement the comparable interface.

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-tdu
koofers-user-tdu 🇺🇸

9 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CMSC 131 Fall 2008
Jan Plane (Adapted from Bonnie Dorr)
Lecture Set #14:
Polymorphism
Introduction
1. Wrappers
2. Interfaces
CMSC 131 Fall 2008
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
pf3
pf4

Partial preview of the text

Download Java Polymorphism: Wrappers and Interfaces in CMSC 131 Fall 2008 - Prof. Bonnie J. Dorr and more Study notes Computer Science in PDF only on Docsity!

CMSC 131 Fall 2008 Jan Plane (Adapted from Bonnie Dorr)

Lecture Set #14:

Polymorphism

Introduction

1. Wrappers

2. Interfaces

CMSC 131 Fall 2008 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 Fall 2008 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 Fall 2008 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 Fall 2008 Jan Plane (Adapted from Bonnie Dorr) 6

Adapting 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 object

CMSC 131 Fall 2008 Jan Plane (Adapted from Bonnie Dorr) 7

What 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

 They do not implement Comparable

 Hence findMin cannot be used on them

 That’s why we use wrappers!