DOC PREVIEW
UMD CMSC 433 - Java and Java Generics

This preview shows page 1-2-3-20-21-40-41-42 out of 42 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CMSC 433: ProgrammingParadigms and TechnologiesFall 2006Java and Java Generics(slides partially developed by Jeff Foster for CS330)CMSC 433, Fall 2006 2Java• Developed in 1995 by Sun Microsystems– Started off as Oak, a language aimed at software forconsumer electronics– Then the web came along...• Java incorporated into web browsers– Java source code compiled into Java byte code– Executed (interpreted) on Java Virtual Machine• Portability to different platforms• Safety and security much easier, because code is notdirectly executing on hardware• These days, Java used for a lot of purposes– Server side programming, general platform, etc.CMSC 433, Fall 2006 3Java Versions• Java has evolved over the years– Virtual machine quite stable, but source languagehas been getting new features• Will use Java 1.5 (a.k.a Java 5.0) for this class– We will be using 1.5-specific features, so if you’ve gota different version, you will want to upgrade– Some of the new features in Java 1.5 came as aresponse to pressure from Microsoft’s C#CMSC 433, Fall 2006 4Object-Orientation• Java is a class-based, object-oriented language• Classes extend other classes to inherit– The root of the inheritance hierarchy is Object– Why have a root of the hierarchy?• Classes also implement interfaces– Interface is like a class with declarations but no code• Classes may extend one other class, but canimplement many interfaces– Multiple inheritance is tricky to understand/implementCMSC 433, Fall 2006 5Subtyping• Both inheritance and interfaces allow one classto be used where another is specified– This is really the same idea: subtyping• We say that A is a subtype of B if– A extends B or a subtype of B, or– A implements B or a subtype of BCMSC 433, Fall 2006 6Liskov Substitution PrincipleIf for each object o1 of type S there is an object o2 oftype T such that for all programs P defined in terms of T,the behavior of P is unchanged when o1 is substituted foro2 then S is a subtype of T.– I.e, if anyone expecting a T can be given an S, thenS is a subtype of T.– Does our definition of subtyping in terms of extendsand implements obey this principle?CMSC 433, Fall 2006 7Polymorphism in Java• Subtyping is a kind of polymorphism– Sometimes called subtype polymorphism– Allows method to accept objects of many types• Another kind: parametric polymorphism– Implemented as generic methods in Java• Ad-hoc polymorphism is overloading– Method overloadingCMSC 433, Fall 2006 8A Stack of Integersclass IntegerStack { class Entry { Integer elt; Entry next; Entry(Integer i, Entry n) { elt = i; next = n; } } Entry theStack; void push(Integer i) { theStack = new Entry(i, theStack); } Integer pop() throws EmptyStackException { if (theStack == null) throw new EmptyStackException(); else { Integer i = theStack.elt; theStack = theStack.next; return i; }}}CMSC 433, Fall 2006 9Inner Classes• Classes can be nested inside other classes– These are called inner classes• Within a class that contains an inner class, youcan use the inner class just like any other classCMSC 433, Fall 2006 10Referring to Outer Class class Stack { ... private int numEntries; class Entry { Integer elt; Entry next; Entry(Integer i) { elt = i; next = null; numEntries++; } } }• Each inner “object” has an implicit reference tothe outer “object” whose method created it– Can refer to fields directly, or use outer class nameCMSC 433, Fall 2006 11Other Features of Inner Classes• Outside of the outer class, use outer.innernotation to refer to type of inner class– E.g., Stack.Entry• An inner class marked static does not have areference to outer class– Can’t refer to instance variables of outer class– Must also use outer.inner notation to refer to innerclass• Question: Can Stack.Entry be made static?CMSC 433, Fall 2006 12Compiling Inner Classes• The JVM doesn’t know about inner classes– Compiled away, similar to generics– Inner class Foo of outer class A producesA$Foo.class– Anonymous inner class of outer class A producesA$1.class• We’ll see these later• Why are inner classes useful?CMSC 433, Fall 2006 13IntegerStack ClientIntegerStack is = new IntegerStack();Integer i;is.push(new Integer(3));is.push(new Integer(4));i = is.pop();• This is OK, but what if we want other kinds ofstacks?– Need to make one XStack for each kind of X– Problems: Code bloat, maintainability nightmareCMSC 433, Fall 2006 14Polymorphism Using Objectclass Stack { class Entry { Object elt; Entry next; Entry(Object i, Entry n) { elt = i; next = n; } } Entry theStack; void push(Object i) { theStack = new Entry(i, theStack); } Object pop() throws EmptyStackException { if (theStack == null) throw new EmptyStackException(); else { Object i = theStack.elt; theStack = theStack.next; return i; }}}CMSC 433, Fall 2006 15Stack ClientStack is = new Stack();Integer i;is.push(new Integer(3));is.push(new Integer(4));i = (Integer) is.pop();• Now Stacks are reusable– push() works the same– But now pop() returns an Object• Have to downcast back to Integer• Not checked until run-timeCMSC 433, Fall 2006 16General Problem• When we move from an X container to anObject container– Methods that take X’s as input parameters are OK• If you’re allowed to pass Object in, you can pass any X in– Methods that return X’s as results require downcasts• You only get Objects out, which you need to cast down to X• This is a general feature of subtypepolymorphismCMSC 433, Fall 2006 17Parametric Polymorphism (for Classes)• In Java 1.5 we can parameterize the Stackclass by its element type• Syntax:– Class declaration: class A<T> { ... }• A is the class name, as before• T is a type variable, can be used in body of class (...)– Client usage declaration: A<Integer> x;• We instantiate A with the Integer typeCMSC 433, Fall 2006 18class Stack<ElementType> { class Entry { ElementType elt; Entry next; Entry(ElementType i, Entry n) { elt = i; next = n;} } Entry theStack; void push(ElementType i) { theStack = new Entry(i, theStack); } ElementType pop() throws EmptyStackException { if (theStack == null) throw new EmptyStackException(); else { ElementType i = theStack.elt; theStack = theStack.next; return i; }}}Parametric Polymorphism for StackCMSC 433, Fall 2006 19Stack<Element>


View Full Document

UMD CMSC 433 - Java and Java Generics

Documents in this Course
Trace 1

Trace 1

62 pages

Reflection

Reflection

137 pages

Testing

Testing

25 pages

Paradigms

Paradigms

10 pages

Testing

Testing

17 pages

Java RMI

Java RMI

17 pages

Java RMI

Java RMI

17 pages

Java RMI

Java RMI

17 pages

Trace 1

Trace 1

46 pages

Jini

Jini

4 pages

Final

Final

15 pages

Java RMI

Java RMI

13 pages

Testing

Testing

16 pages

Load more
Download Java and Java Generics
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Java and Java Generics and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Java and Java Generics 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?