Why Generic Programming? Core Java

Generic programming means to write code that can be reused for objects of many differenttypes. For example, you don’t want to program separate classes to collect String and Fileobjects. And you don’t have to—the single class ArrayList collects objects of any class.

This is one example of generic programming.

Before Java SE 5.0, generic programming in Java was always achieved with inheritance.

The ArrayList class simply maintained an array of Object references:

This approach has two problems. A cast is necessary whenever you retrieve a value:

Moreover, there is no error checking. You can add values of any class:

This call compiles and runs without error. Elsewhere, casting the result of get to a Stringwill cause an error. Generics offer a better solution: type parameters. The ArrayList class now has a typeparameter that indicates the element type:

This makes your code easier to read. You can tell right away that this particular arraylist contains String objects. The compiler can make good use of this information too. No cast is required for callingget. The compiler knows that the return type is String, not Object:

The compiler also knows that the add method of an ArrayList<String> has a parameter oftype String. That is a lot safer than having an Object parameter. Now the compiler cancheck that you don’t insert objects of the wrong type. For example, the statement

will not compile. A compiler error is much better than a class cast exception at runtime.This is the appeal of type parameters: they make your programs easier to read and safer.

Who Wants to Be a Generic Programmer?

It is easy to use a generic class such as ArrayList. Most Java programmers will simplyuse types such as ArrayList<String> as if they had been built into the language, just likeString[] arrays. (Of course, array lists are better than arrays because they can expandautomatically.)

However, it is not so easy to implement a generic class. The programmers who use yourcode will want to plug in all sorts of classes for your type parameters. They expecteverything to work without onerous restrictions and confusing error messages. Your jobas a generic programmer, therefore, is to anticipate all the potential future uses of your class.

How hard can this get? Here is a typical issue that the designers of the standard classlibrary had to grapple with. The ArrayList class has a method addAll to add all elements ofanother collection. A programmer may want to add all elements from an ArrayList<Manager>to an ArrayList<Employee>. But, of course, doing it the other way around should not belegal. How do you allow one call and disallow the other? The Java language designersinvented an ingenious new concept, the wildcard type, to solve this problem. Wildcardtypes are rather abstract, but they allow a library builder to make methods as flexible aspossible.

Generic programming falls into three skill levels. At a basic level, you just use genericclasses—typically, collections such as ArrayList—without thinking how and why theywork. Most application programmers will want to stay at that level until somethinggoes wrong. You may encounter a confusing error message when mixing differentgeneric classes, or when interfacing with legacy code that knows nothing about typeparameters. At that point, you need to learn enough about Java generics to solve problemssystematically rather than through random tinkering. Finally, of course, you maywant to implement your own generic classes and methods.

Application programmers probably won’t write lots of generic code. The folks at Sunhave already done the heavy lifting and supplied type parameters for all the collectionclasses. As a rule of thumb, only code that traditionally involved lots of casts from verygeneral types (such as Object or the Comparable interface) will benefit from using typeparameters.

In this section, we tell you everything you need to know to implement your owngeneric code. However, we expect most readers to use this knowledge primarily forhelp with troubleshooting, and to satisfy their curiosity about the inner workings of the parameterized collection classes.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Core Java Topics