Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com."— Presentation transcript:

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

2 CS-2852 Data Structures, Andrew J. Wozniewicz Agenda Abstract Data Types (ATDs) Interfaces Abstract Classes Generics Collection List

3 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

4 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

5 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

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

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

8 CS-2852 Data Structures, Andrew J. Wozniewicz Why Abstract Classes?

9 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.

10 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

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

12 CS-2852 Data Structures, Andrew J. Wozniewicz Implementing an Interface public class MyObject implements Comparable { boolean less(Object m) {...}; boolean greater(Object m) {...}; boolean lessEqual(Object m) {...}; boolean greaterEqual(Object m) {...}; }

13 CS-2852 Data Structures, Andrew J. Wozniewicz Using an Interface MyObject myObject = new MyObject(); if (myObject.less(x))...; Comparable c = null; c = myObject; if (c.less(x))...;

14 CS-2852 Data Structures, Andrew J. Wozniewicz Why Interfaces?

15 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.

16 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.

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

18 CS-2852 Data Structures, Andrew J. Wozniewicz Interfaces versus Abstract Classes Classes represent attributes (data, variables, fields) and capabilities/responsibil ities (operations, methods, functions) Single inheritance (is-a) Interfaces are only about capabilities (operations). Multiple inheritance for classes.

19 CS-2852 Data Structures, Andrew J. Wozniewicz Interfaces vs. Classes II You can implement many interfaces, but be only one class.

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

21 CS-2852 Data Structures, Andrew J. Wozniewicz Generics A 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.

22 CS-2852 Data Structures, Andrew J. Wozniewicz Defining a Generic Class and Methods public class MyGenClass { public MyGenClass () {... } public void MyGenClass (T t) { // }

23 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 extends Iterable

24 CS-2852 Data Structures, Andrew J. Wozniewicz

25 The Collection Interface public interface Collection extends Iterable { // Basic operations int size(); boolean isEmpty(); boolean contains(Object element); boolean add(E element); //optional boolean remove(Object element); //optional Iterator iterator(); // Bulk operations boolean containsAll(Collection c); boolean addAll(Collection c); //optional boolean removeAll(Collection c); //optional boolean retainAll(Collection c); //optional void clear(); //optional // Array operations Object[] toArray(); T[] toArray(T[] a); } UnsupportedOperationException

26 CS-2852 Data Structures, Andrew J. Wozniewicz List (“Sequence”) Interface public interface List extends Collection { // 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 c); //optional // Search int indexOf(Object o); int lastIndexOf(Object o); // Iteration ListIterator listIterator(); ListIterator listIterator(int index); // Range-view List subList(int from, int to); }

27 CS-2852 Data Structures, Andrew J. Wozniewicz Bounded & Wildcard Type Parameters

28 CS-2852 Data Structures, Andrew J. Wozniewicz Bounded & Wildcard Type Parameters 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

29 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

30 Questions? Image copyright © 2010 andyjphoto.com


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

Similar presentations


Ads by Google