Download Week 5 (September 30)

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CSC 520 – Advanced Object
Oriented Programming, Fall, 2010
Thursday, September 30
Week 5, Generics and Inheritance
Techniques, Meyer Ch. 10 & 16
Generic Interfaces / Classes
• We have been using generic interfaces and
classes from the library.
• java.util.Set<E>, java.util.HashSet<E>
• java.util.Map<K,V>, java.util.HashMap<K,V>
• They allow containers to contain different
types (classes) of objects.
• Generic parameters such as E (element type),
K (key type) and V (value type) above are
types bound by client code at compile time.
Using Generic Classes
• Client code supplies a binding for each generic
parameter that must be an interface or class
name.
• It cannot be a primitive type.
• A generic parameter may constrain the type of
class or interface that client code can bind
using a wildcard.
• ?, ? extends T, ? super T – details follow.
• These are Meyer’s constrained generics (16.4).
Building Generics
• A class or interface specifies its generic type.
•
•
•
•
•
HashSet<E>, HashMap<K, V>, etc.
? extends T is a bounded wildcard, T or a subclass
? super T is a lower-bounded wildcard, T or a superclass
? is an unbounded wildcard, same as ? Extends Object
The bound constrains client code type binding.
• The writer of a generic class must ensure type
consistency within the class.
• The user can safely assume type compatibility if the
compiler does not flag an error.
Erasure and Restrictions
• Unlike C++ template classes, the compiler
generates only 1 copy of the class code for a
generic class, erasing the generic type T
information at run time.
• Every generic type T is treated as a java.lang.Object at
run time.
• Writers of generic classes may need to cast to generic
type T, E, K, V etc. in places.
• Writers of generic classes must ensure that those casts
are valid. The compiler cannot ensure this.
Restrictions
• 1. No constructor – no “new T()” for generic T.
• 2. No “new T[]” – you must cast an “Object []” to a “T
[]” within the generic class code.
• This cast often fails at run time with a wildcard T. Make
sure to test if you use arrays of wildcard generic types.
• 3. No generic T in a static field, static method or
static initialization block of code.
• 4. Exception classes cannot be generic.
• parson/JavaLang/mapgame/Mymap.java
C++ Template classes
• A template class is a macro-like cookie cutter.
• The compiler (and possibly linker) create a
distinct set of source methods in memory for
every binding of the generic type (create all
used methods for list<int>, list<string>, etc.)
• Cyclic cooperation between compiler and linker in the
old days, went out the window with Microsoft.
• See http://cplusplus.com/ STL reference.
Polymorphism
• Subclass polymorphism
• O-O inheritance allows a base interface or base class to
take many forms in derived interfaces or classes
• Explicit parametric polymorphism
• Java, C++ or similar generic classes with type
parameters.
• Implicit parametric polymorphism
• Python and other dynamically-typed languages allow
multiple types to bind to operations and function
parameters, return results, variables and fields.
Some Inheritance Issues from
Meyer Ch. 16
• Parents’ Invariant Rule
• The invariants of all the parents of a class apply to the class itself.
• Assertion Redeclaration Rule (1)
• A routine (method) redeclaration may only replace the original
precondition by one equal or weaker, and the original
postcondition by one equal or stronger.
• Universal Class Rule is Java’s java.lang.Object.
• Meyer’s frozen is Java final modifier, C++ const.
• Java and C++ provide class static methods and fields.
• Meyer also covers RTTI (run-time type identification) as related to
guarded down casting.