Download presentation
Presentation is loading. Please wait.
1
CS 106 Introduction to Computer Science I 04 / 27 / 2007 Instructor: Michael Eckmann
2
Michael Eckmann - Skidmore College - CS 106 - Spring 2007 Today’s Topics questions, comments? Designing for polymorphism discussion ArrayLists
3
Designing for polymorphism Polymorphism allows a consistent approach to inconsistent but related behaviors. What does that mean? First examine the problem to find opportunities that lend themselves to this before we write code. Let's look at some example situations and discuss them.
4
Designing for polymorphism Different types of vehicles move in different ways All business transactions for a company must be logged All products produced by a company must meet certain quality standards A hotel needs to plan their remodeling efforts for every room A casino wants to analyze the profit margin for their games A dispatcher must schedule moving vans and personnel based on the job size A drawing program allows the user to draw various shapes
5
ArrayList This is a good time to bring up a class in Java API named ArrayList, because it works on general Objects. ArrayList is a class in the Java API It can store different types of data into an “array” An ArrayList can change size throughout the lifetime of the program – whereas a regular array is a fixed size An ArrayList actually stores references to objects, not the objects themselves We cannot store values of primitive types directly -- we have to use the wrapper classes if we want to do that (we'll see what this means later).
6
ArrayList Some ArrayList methods – ArrayList() --- constructor – boolean add(Object o) – add the object reference to the end of the list – void add(int index, Object o) – add the object reference to the list at the index – void clear() -- remove all elements from the list – Object get(int index) – return the object reference at that index in the list – int indexOf(Object o) – returns the index of the first occurrence of o – boolean isEmpty() -- returns true if the list contains no elements, false otherwise
7
ArrayList Some ArrayList methods – Object remove(int index) – returns the object reference at that index in the list and removes it from the list – int size() – returns the number of elements in the list – boolean contains(Object o) -- returns true if the list contains o, false otherwise – void ensureCapacity(int minCapacity) – tells Java to ensure that the list will be able to contain at least minCapacity references – int lastIndexOf(Object o) – what do you think this does? – boolean remove(Object o) -- removes a single instance of o from the list – void removeRange(int fromIndex, int toIndex) --removes from the list all of the elements whose index is between fromIndex, inclusive and toIndex, exclusive.
8
ArrayList Some ArrayList methods – Object set(int index, Object o) -- replaces the element at the specified index in this list with o. – What if this method wasn't in the class. How might you achieve that functionality?
9
ArrayList Let's create an ArrayList of different types and add some elements to it and remove some, etc... When we create one that allows different types, we have to cast any returned Objects from methods to be the type we want. Because Java implemented ArrayLists generically, and because of the way Java decided to have the class hierarchy such that all classes have Object as their superclass, this usefulness of ArrayLists is dramatically increased over a class that may have only allowed one type for its elements. So, that's a large benefit of good object oriented design --- generality --- so that you (or the Java implementers themselves) don't have to implement multiple classes to work on multiple types. If you create your class hierachy well, lots of stuff can be done generically (e.g. Have code that works on all Pets instead of just Dog or Cat objects for instance.)
10
ArrayList If we stored several different types of data in a list, we may not know at some point what type of object is actually stored in a particular index of the ArrayList. How do we find out what type a particular object actually is?
11
ArrayList If we stored several different types of data in a list, we may not know at some point what type of object is actually stored in a particular index of the ArrayList. How do we find out what type a particular object actually is? We can use the instanceof operator to check if some reference is an instance of some type. e.g. Object o = somearraylist.get(0); if (o instanceof Contact) { //then cast o to be a Contact and use it }
12
ArrayList Let's look at how each method affected the list and what they returned. – ArrayList(), boolean add(Object o), – void add(int index, Object o), void clear(), Object get(int index), – int indexOf(Object o), boolean isEmpty(), – Object remove(int index), int size(), boolean contains(Object o), – void ensureCapacity(int minCapacity), – int lastIndexOf(Object o), boolean remove(Object o), – protected void removeRange(int fromIndex, int toIndex)
13
Polymorphism via Inheriance An ArrayList uses polymorphism because it holds references to objects of Object. An Object reference can be used to refer to any object (of any type)!!!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.