DOC PREVIEW
TRINITY CSCI 1321 - Generics, Enums, and Inner Classes

This preview shows page 1-2-3-4 out of 12 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 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 12 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 12 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 12 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1Generics, Enums, and Inner Classes2/5/20082Opening Discussion■Let's look at solutions to the interclass problem.■“Keeping up” with code in class.■Why don't I teach physics classes?■Do you have any questions about the reading?■Do you have any questions about the assignment?3Inheritance Example■The examples we started last time we will develop some more in the future, but I want to run you through an example of using inheritance quickly.■This example is pulled from functional languages. It want to abstract the concept of a function.■Let's create an interface called MathFunction that can be used to map numbers to other numbers. Then we can write a few subclasses of it and demonstrate the use of these in our code.4Inner Classes■You can embed classes inside of code in Java. Not all classes have to be at the global scope. Classes that aren't at the global scope are called inner classes.■There are three types of inner classesNormal – Sits at the class scope. Looks normal.Local – Sits in a method. Looks normal. Rarely used.Anonymous – Sits in a method and has a syntax that doesn't name the class. Class must inherit from another class.■Each type can either be static or not. Things should be static by default. Non-static knows about the object that created it.5More Function Fun■It turns out that our little MathFunction interface makes a perfect supertype for creating inner classes.■We can demonstrate this by thinking of how we can create functions from other functions.■Let's come up with a few of those and write code to do them.6More General Functions■The MathFunctions can be fun, but they are a bit limited because they can only work with doubles.■How would we write code that can do more general mapping? I want code that basically works with ANY type.■Let's create an interface called GeneralFunction to represent this.7Java 5.0 Features■Java 5.0 adds quite a few features to the Java language that make it easier to write real code in Java. Most of class today we will focus on two big ones, generics and enums. There are a few others worth mentioning.■Autoboxing – primitive types are not objects, but sometimes you need to treat them that way. This automates the process.■Varargs – Java now has syntax for passing a variable number of arguments.■Foreach loop – A handy shortcut for when you want to do something with all elements of a container.8Generics■The most significant feature added to Java 5.0 was that of generics. Generics provide a form of parametric polymorphism, typically for code that can take any type, but might be limited to a specific type for one instance.■The most common use of this is for containers as container classes typically should be able to hold anything, but any one container is generally intended to hold only one type.■In practice, generics give you extra type safety and prevent you from doing a lot of type casts.9Making General Typesafe■The problem with using the Object type for general polymorphism is that it many different type checks have to be done at runtime and you lose static type safety.■Using generics we can take our general function interface and make it static typesafe.■How can you combine these more general types of functions?10Enums■C had enums. What were they supposed to do? What was the problem with them?■Java includes enums as well. They serve the same goals, but lack the pitfalls.■Java enum syntax can get quite complex, but the basic form is simple and very similar to C.11Code■Generics play a big role in the project and appear in the first assignment so I want to show you a bit of the syntax.■I'm going to write some code that uses generics so show the syntax. I'd rather you not try to follow along with this coding at the end of class and instead just try to pay close attention to what I'm doing, the logic behind it, and the syntax required to make it work. You can see the code on the web or even pull it down so you can play with it later.12Minute Essay■What is the purpose of generics?■The design for assignment #1 is due today.■Interclass Problem – Take the general function code that was written in class today and extend it. Write three different new functions. At least one must be an anonymous inner class. Prove they all work with test


View Full Document

TRINITY CSCI 1321 - Generics, Enums, and Inner Classes

Documents in this Course
Recursion

Recursion

11 pages

Iterators

Iterators

10 pages

Actors

Actors

9 pages

Recursion

Recursion

15 pages

Recursion

Recursion

10 pages

Threads

Threads

7 pages

Trees

Trees

11 pages

Load more
Download Generics, Enums, and Inner Classes
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 Generics, Enums, and Inner Classes 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 Generics, Enums, and Inner Classes 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?