cs-2852 data structures lecture 2 andrew j. wozniewicz image copyright © 2010 andyjphoto.com

30
CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

Upload: kathlyn-frederica-rice

Post on 18-Jan-2018

219 views

Category:

Documents


0 download

DESCRIPTION

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

TRANSCRIPT

Page 1: 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

Page 2: CS-2852 Data Structures LECTURE 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

Page 3: CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

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

Page 4: CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

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

Page 5: CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

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

Page 6: CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

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}

Page 7: CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

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);

Page 8: CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

Why Abstract Classes?

Page 9: CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

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.

Page 10: CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

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

Page 11: CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

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);}

Page 12: CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

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)

{...};}

Page 13: CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

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)) ...;

Page 14: CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

Why Interfaces?

Page 15: CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

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.

Page 16: CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

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.

Page 17: CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

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!

Page 18: CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

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.

Page 19: CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

Interfaces vs. Classes II

You can implement many interfaces, but be only one class.

Page 20: CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

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!!!

Page 21: CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

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.

Page 22: CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

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> }

}

Page 23: CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

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>

Page 24: CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

Page 25: CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

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

Page 26: CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

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);}

Page 27: CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

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>

Page 28: CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

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

Page 29: CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

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

Page 30: CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

Questions?

Image copyright © 2010 andyjphoto.com