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

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

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

Unformatted text preview:

Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 141Generics, Enums, and Inner Classes1/29/20092Opening Discussion■Let's look at solutions to the interclass problem.■“Keeping up” with code in class. Scribes too slow.■Do you have any questions about the reading?■Do you have any questions about the assignment?3Restrictions in Java■Java places some restrictions on inheritance to simplify the language. The main restriction is that you can only extend one class. Doing otherwise, multiple inheritance, tends to make things very complex.■There are times when you want to have a class be a subtype of two different types though. To allow this Java has a construct called an interface. Interfaces have no data (they can have static data) and all methods in them are abstract. They only define what you can do with them, not how to do it. You can implement as many interfaces as you want.4Interfaces Continued■Java allows multiple inheritance from interfaces because they can never create ambiguity.■Implementing an interface only provides subtyping, not code reuse.■Subtypes of interfaces need to implement all of the methods of that interface or they will be abstract.5Inheritance 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.6Inner 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/interface.■Each type can either be static or not. Things should be static by default. Non-static knows about the object that created it.7More 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.8More 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.9Java 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.10Generics■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. 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.11Making 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?12Enums■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.13Code■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.14Minute Essay■What is the purpose of generics?■Next class you will take the first quiz.■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?