CS-2852 Data Structures LECTURE 11 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
-
Upload
kathlyn-frederica-rice -
Category
Documents
-
view
219 -
download
0
description
Transcript of CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
CS-2852Data StructuresLECTURE 2
Andrew J. Wozniewicz
Image copyright © 2010 andyjphoto.com
CS-2852 Data Structures, Andrew J. Wozniewicz
Agenda• Abstract Data Types (ATDs)• Interfaces• Abstract Classes• Generics
• Collection• List
CS-2852 Data Structures, Andrew J. Wozniewicz
Abstract Data Type
• A mathematical construct/model.• A pure abstraction: no implementation
details whatsoever.• Preconditions & postconditions
A set of values and a set of operations on those values.
DEFINITION
CS-2852 Data Structures, Andrew J. Wozniewicz
ADT Examples• STACK:– Push(x)– x <- Pop()
• QUEUE: – Enqueue(x)– y <- Dequeue()
• LIST:– Add(x)– x <- Remove()– X <- getAt(n)– setAt(n,X)– b <- Contains(X)
LIFO
FIFO
CS-2852 Data Structures, Andrew J. Wozniewicz
Abstract Classes
• Cannot be instantiated.• May or may not include abstract
methods.• May or may not include concrete
methods.
A class that is declared abstract.DEFINITION
CS-2852 Data Structures, Andrew J. Wozniewicz
Abstract Class Example
public abstract class GraphicObject { // declare fields... // declare non-abstract methods... abstract void draw(); //<-a. method}
CS-2852 Data Structures, Andrew J. Wozniewicz
Abstract Method
• Declared without an implementation.• Followed by semicolon; no braces.• Any class containing abstract methods
must be declared abstract.
An abstract method is one prefixed with the keyword abstract.
DEFINITION
abstract void moveTo(double deltaX, double deltaY);
CS-2852 Data Structures, Andrew J. Wozniewicz
Why Abstract Classes?
CS-2852 Data Structures, Andrew J. Wozniewicz
Why Abstract Classes?• Can be subclassed (extended; inherited
from).• Forms a contract; a promise to deliver
certain functionality.• Subclasses are obligated to override
methods that are marked as abstract.• Allows for (partial) sharing of
implementations.
CS-2852 Data Structures, Andrew J. Wozniewicz
Interface
• Empty method bodies• All interface methods are implicitly
public and abstract• Extensible, just like classes• Multiple inheritance is supported
A named collection of method declarations (without implementations).
DEFINITION
CS-2852 Data Structures, Andrew J. Wozniewicz
Example of an Interface
public interface Comparable {
boolean less(Object m);boolean greater(Object m);boolean lessEqual(Object m);boolean greaterEqual(Object
m);}
CS-2852 Data Structures, Andrew J. Wozniewicz
Implementing an Interfacepublic class MyObject implements Comparable{
boolean less(Object m) {...};boolean greater(Object m) {...};boolean lessEqual(Object m) {...};boolean greaterEqual(Object m)
{...};}
CS-2852 Data Structures, Andrew J. Wozniewicz
Using an InterfaceMyObject myObject = new MyObject();if (myObject.less(x)) ...;
Comparable c = null;c = myObject;if (c.less(x)) ...;
CS-2852 Data Structures, Andrew J. Wozniewicz
Why Interfaces?
CS-2852 Data Structures, Andrew J. Wozniewicz
Why Interfaces?• A development contract. It ensures
that a particular object satisfies a given set of methods.
• Coding to interfaces is a technique by which developers can expose certain methods of an object to other objects in the system.
• Ability to code to the interface in place of coding to the object itself.
CS-2852 Data Structures, Andrew J. Wozniewicz
Advantages of Interfaces• Design: the methods of an object can be quickly
specified and published to all affected developers • Development: the Java compiler guarantees that all
methods of the interface are implemented with the correct signature and that all changes to the interface are immediately visible to other developers
• Integration: there is the ability to quickly connect classes or subsystems together, due to their well-established interfaces
• Testing: interfaces help isolate bugs because they limit the scope of a possible logic error to a given subset of methods.
CS-2852 Data Structures, Andrew J. Wozniewicz
Interface Caveats• Additional typing of code.• Some (minimal) run-time overhead
due to required code infrastructure.• This overhead is insignificant when
compared to the ease and benefit of using interfaces.
USE THEM!
CS-2852 Data Structures, Andrew J. Wozniewicz
Interfaces versus Abstract Classes
• Classes represent attributes (data, variables, fields) and capabilities/responsibilities (operations, methods, functions)
• Single inheritance (is-a)
• Interfaces are only about capabilities (operations).
• Multiple inheritance for classes.
CS-2852 Data Structures, Andrew J. Wozniewicz
Interfaces vs. Classes II
You can implement many interfaces, but be only one class.
CS-2852 Data Structures, Andrew J. Wozniewicz
When to use Interfaces?
A strong sign that you need to introduce an interface is that you have very similar code in separate classes and you can't re-arrange the classes to inherit this behaviour from a common superclass.
USE THEM!!!
CS-2852 Data Structures, Andrew J. Wozniewicz
GenericsA concept/mechanism that applies to type declarations and methods,
and allows for type parameters.
DEFINITION
• An invocation of a generic type is generally known as a parameterized type.
• Interfaces, methods, and ctors may be generic.
CS-2852 Data Structures, Andrew J. Wozniewicz
Defining a Generic Class and Methods
public class MyGenClass<T>{
public MyGenClass<T>() { ... }
public void MyGenClass<T>(T t) { // <do something with t> }
}
CS-2852 Data Structures, Andrew J. Wozniewicz
Collection Interface• Represents a group of objects, known as
its elements. • Some collections allow duplicate elements
and others do not.• Some are ordered and others unordered.• No direct implementation in JDK.• Inherits from Iterable.
public interface Collection<E> extends Iterable<E>
CS-2852 Data Structures, Andrew J. Wozniewicz
CS-2852 Data Structures, Andrew J. Wozniewicz
The Collection Interfacepublic interface Collection<E> extends Iterable<E> { // Basic operations
int size(); boolean isEmpty(); boolean contains(Object element); boolean add(E element); //optional boolean remove(Object element); //optional Iterator<E> iterator();
// Bulk operations
boolean containsAll(Collection<?> c); boolean addAll(Collection<? extends E> c); //optional boolean removeAll(Collection<?> c); //optional boolean retainAll(Collection<?> c); //optional void clear(); //optional
// Array operations
Object[] toArray(); <T> T[] toArray(T[] a);}
UnsupportedOperationException
CS-2852 Data Structures, Andrew J. Wozniewicz
List (“Sequence”) Interfacepublic interface List<E> extends Collection<E> { // Positional access
E get(int index); E set(int index, E element); //optional boolean add(E element); //optional void add(int index, E element); //optional E remove(int index); //optional boolean addAll(int index, Collection<? extends E> c); //optional
// Search
int indexOf(Object o); int lastIndexOf(Object o);
// Iteration
ListIterator<E> listIterator(); ListIterator<E> listIterator(int index);
// Range-view
List<E> subList(int from, int to);}
CS-2852 Data Structures, Andrew J. Wozniewicz
Bounded & Wildcard Type Parameters
• <U extends Number>• <U extends Number & MyInterface>• <? extends Number, MyInterface>• <? extends Number>• <? extends Number, Data>
CS-2852 Data Structures, Andrew J. Wozniewicz
Bounded & Wildcard Type Parameters
• <U extends Number>• <U extends Number & MyInterface>• <? extends Number, MyInterface>• <? extends Number>• <? extends Number, Data>
All classes, interfaces, and enum types can be used as type parameter bound, including nested and inner types. Neither primitive types nor array types be used as type parameter bound.
BOUND
WILDCARD
CS-2852 Data Structures, Andrew J. Wozniewicz
Summary• ATDs – Purely theoretical (math).• Interfaces – A named set of methods.• Abstract Classes – Partially
implemented.• Generics – Parametrized types/methods
for safety/robustness, readability.
• Collection and List interfaces
Questions?
Image copyright © 2010 andyjphoto.com