Posted by : Rhyf Ahmad Saturday, May 23, 2015

A generic type, which is also referred to as a parameterized type, is a class or interface type definition that has one or more type parameters. You defi ne an actual class or interface type from a generic type by supplying a type argument for each of the type parameters that the generic type has. It'll be easier to understand what this means with a concrete example of where and how you could apply the concept. I'm sure you recall the LinkedList class that you fi rst saw in Chapter 6 and used in an example in Chapter 12.
You used the LinkedList class to encapsulate a linked list of Point objects, but the idea of a linked list applies to any set of objects that you want to organize in this way. A linked list is just one example of classes that defi ne objects for organizing other objects of a given type into a collection in some way. Such classes are described as collection classes for obvious reasons, and in Chapter 14 you see a variety of these that are defi ned in the java.util package. Of course, you have already seen the EnumSet collection class in use for fi le I/O, and you learn more about this class later in Chapter 14, too. The LinkedList class that you implemented in Chapter 6 can organize objects of any given type into
a linked list. This clearly has the advantage that the code for a single class defi nes a linked list class that you can use for objects of any kind, but it has signifi cant disadvantages, too. When you were adding Point objects to a LinkedList object, nothing in the code prevented you from adding a Line object, or indeed any type of object, to the same linked list. Of course, if you were to do this inadvertently, the result would be a disaster, because when you retrieved objects from the list, you would not know that some of the objects were not of type Point. If you attempt to use an object as a Point object that was actually type Line or type String, your program fails.

Contents:

What Are Generic Types?
Defining a Generic Class Type

  1. Implementing a Generic Type
  2. Instantiating a Generic Type
  3. The Runtime Type of Generic Type Instances
  4. Relationships between Generic Type Instances
  5. Multiple Type Parameters
  6. Type Parameter Scope
  7. Static Fields in a Generic Type
  8. Type Parameter Bounds
Generic Types and Generic Interfaces

  1. Enabling the Collection-Based for Loop for a Container Class
  2. Implementing an Iterator Capability
  3. A Parameterized Type for Binary Trees
Variables of a Raw Type
Wildcards as Type Parameter Arguments

  1. Constraints on a Wildcard
  2. More on the Class Class
Arrays and Parameterized Types
Parameterized Methods

  1. Generic Constructors
Parameterized Types and Inheritance 



Ebook Preview
Get the ebook now (1.31 MB)

If the link above doesn't work, try the link below


Mirror: Get the ebook now (1.31 MB)

Leave a Reply

You can leave a comment to request any article(s), software(s), ebook(s) or report the dead link.
Then I will reply as soon as I can :)

Subscribe to Posts | Subscribe to Comments

Welcome to My Blog

Recent Post

Recent Posts Widget
Instagram

- Copyright © Madriva -Robotic Notes- Powered by Blogger - Designed by Johanes Djogan -