Generic Programming - PowerPoint PPT Presentation

About This Presentation
Title:

Generic Programming

Description:

Need for Java's Object type and generics. Bag Sequence Stack ... Every class in Java is directly or indirectly (if the class extends another ... – PowerPoint PPT presentation

Number of Views:288
Avg rating:3.0/5.0
Slides: 29
Provided by: fen110
Category:

less

Transcript and Presenter's Notes

Title: Generic Programming


1
Generic Programming
  • Object Type
  • Autoboxing
  • Bag of Objects
  • JCL Collections
  • Nodes of Objects
  • Iterators

2
Ch. 5 Java Objects and Iterators
  • Javas Object type
  • Wrapper classes for primitive data types
  • Objects collection is actually references
    collection
  • Node object that contains object data
  • Iterator

3
Need for Javas Object type and generics
  • Bag Sequence Stack
  • ADTs and data structures where essentially the
    same design can be used for data of different
    types (or even mixed types)
  • for each type
  • re-write essentially the same code
  • minimal modification with respect to the type for
    this code

4
Javas Object type
  • Javas Object class type provides a possibility
    for writing one code and using it for any type
    (or all types

5
Object class and inheritance
  • Every class in Java is directly or indirectly (if
    the class extends another class) derived from the
    Object class,, i.e., extending the Object class.
    So, every object is an object of Object class
  • String snew String(Something)
  • Object obj
  • obj s

6
Class type conversion
  • If an object is being treated as an object of
    Object class, using this object reference we can
    not access the methods and member variables
    defined in its original class.
  • The reference needs to be converted to the
    original type by casting
  • String snew String(Something)
  • Object obj
  • obj s
  • s (String) obj

7
Conversion of object reference
  • Widening conversion example
  • String str new String (Hello, World!)
  • Object obj str
  • int stringLength str.length( )
  • stringLength obj.length( )
  • Narrowing conversion example
  • String string (String) obj
  • stringLength string.length( )

Answer 13
Illegal Object class has no length method
Answer 13, again
8
Java 1.5 Autoboxing
  • Java has 8 primitive (non-object) types of data
  • integers int, long, short, byte
  • fractionals double, float
  • logicals boolean
  • characters char

9
Wrapper Classes
  • There are 8 classes in Java specifically for
    making an object out of a value of any of the
    primitive data type, and providing additional
    processing tools
  • Integer Float
  • Long Boolean
  • Short Character
  • Byte Double

10
Boxing and Unboxing
  • Autoboxing conversion primitive value converted
    to a wrapper object of corresponding type
  • Unboxing wrapper object is converted to
    corresponding primitve

11
Autoboxing and Unboxing
  • //boxing
  • int i 42
  • int j
  • Integer example
  • Example new Integer(i)
  • j example.intValue()
  • //autoboxing
  • examplei //autoboxing
  • jexample //autounboxing

12
Objects example
  • Really collection of references to objects
  • class ArrayBag
  • object is a bag of objects of any type
  • implemented on an array of Object type
  • Object data

13
Java Objects
  • A collection of objects is actually a collection
    of references to objects
  • Each element of the data array is a reference to
    an object
  • Some of them may refer to the same object

14
Same Object reference example
  • add the same reference into the bag several times
  • each time a copy of the reference is stored into
    a different position of the array
  • all are references to the same object, no new
    copy of the actual object has ever been made

15
object equality
  • and ! tests for objects
  • only test if two references refer to the same
    object
  • not if they have the same content
  • two distinct objects can have the same content.
  • provide test of equality of contents, write an
    equals method in which the object contents are
    compared

16
null value
  • legal value for any class type
  • most primitive data types have nothing similar.
  • code with objects needs to deal with it
  • source of NullPointer-Exception
  • frustration, or error in answers

17
Node object that contain object data
  • A node object can be used in a linked list
  • Instance variables of the IntNode class
  • private int data
  • private IntNode link
  • Instance variables of the general Node class
  • private Object data
  • private IntNode link

18
Generic method vs Object Method
  • static Object middle(Objectdata)
  • if (data.length )
  • return null
  • else
  • return
  • datadata.length
  • static ltTgt T middle(T data)
  • if (data.length )
  • return null
  • else
  • return
  • datadata.length

19
Generic method restrictions
  • Data type inferred must always be a class type
    (not a primitive)
  • May not call a constructor for the generic type
  • Nor may you create a new array of element of that
    type

20
GENERIC CLASSES
  • public class ArrayBagltEgt implements Cloneable
  • private E data
  • private int manyItems
  • ArrayBagltStringgt sbag new ArrayBagltStringgt()
  • ArrayBagltIntegergt sbag new ArrayBagltIntegergt()

21
Generic Class Restrictions
  • Type used to instantiate must be a class not
    primitive
  • Within class cant call constructor for generic
    type (nor make array of elements of that type)

22
Array Bag
  • A generic bag class
  • http//southwestern.edu/owensb/CS2Sp08/Lectures/
    NewBag/ArrayBag.java
  • A silly program to use it
  • http//southwestern.edu/owensb/CS2Sp08/Lectures/N
    ewBag/Author.java

23
Generic Node
  • public class IntNode
  • private int data
  • IntNode linke
  • public class ltEgt Node
  • private E data
  • Node ltEgt link

24
Equals
  • public static ltEgt NodeltEgt listSearch(NodeltEgt
    head, E target)
  • NodeltEgt cursor
  • if (target null)
  • // Search for a node in which the data
    is the null reference.
  • for (cursor head cursor ! null
    cursor cursor.link)
  • if (cursor.data null)
  • return cursor
  • else
  • // Search for a node that contains the
    non-null target.
  • for (cursor head cursor ! null
    cursor cursor.link)
  • if (target.equals(cursor.data))
  • return cursor
  • return null

25
Iterator and Enumeration
26
Iterator Code
  • A class that implements the generic Iterator must
    provide these three methods
  • public boolean hasNext ( )
  • public E next ( )
  • public void remove ( )

27
Interface vs. Class
  • interface methods are un-implemented originally
  • objects of an interface cannot be created, until
    some class implements this interface and its
    methods
  • then an object of this class can be used as an
    object of this interface

28
Interface
  • common standard shared between the user/invoker
    of Java programs and the program designer
  • documentation of an interface specifies clearly
    the properties of an object of this interface
  • whatever class the object belongs user can use
    it confident of those properties
Write a Comment
User Comments (0)
About PowerShow.com