

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


CMSC 131 Spring 2009 Jan Plane (Adapted from Bonnie Dorr)
CMSC 131 Spring 2009 Jan Plane (Adapted from Bonnie Dorr)
1
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
CMSC 131 Spring 2009 Jan Plane (Adapted from Bonnie Dorr)
3
Code Re-use
Many operations recur in programming
Desirable: one implementation!
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
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
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