about how Java generics are used ....

Source: Internet
Author: User
Tags class definition comparable

Transferred from: Http://onewebsql.com/blog/generics-extends-super

The following are basically enough

Today We continue our mini-series on Java generics. In previous posts we have investigated

    • How to crash your IDE using Java wildcards,
    • What beginners find surprising when creating parametrized objects, and
    • What is the difference between Java arrays and generic lists.

Java type parameters is used as type placeholders.

?
1 publicclassList<X>  { }

A list<x> is a container for X objects, and x can being instantiated with any class:you can Havelist<object>, list<string>, and list<number>.

Introducing Bounds:extends

You often want to restrict the set of types the can is used in instantiation. If you create a class Garage, you want it to the hold only Vehicle objects.

The syntax you use in Java are like this:

?
1 publicclass Garage<X extendsVehicle> { }

Every time you instantiate the Garage, the type parameter have to be a subclass of Vehicle.

?
123 classCar extends Vehicle { }class Motorcycle extends Vehicle { }class Fruit extendsObject { }

Thus garage<car> and garage<motorcycle> are OK but garage<fruit> is Not OK.

You can specify more than one bound with extends:

?
1234 classVehicle { }interface PassengerVehicle { }interface MotorVehicle { }class ParkingGarage<X extendsVehicle & MotorVehicle & PassengerVehicle>

You can specify at the very one class in the bound (obviously, as can only inherit from one class in Java) and As many interfaces as you want.

The bound can refer to the typing parameter:

?
1 classBST<X extendsComparable<X>> {}

BST class can only is instantiated with classes X which implement the comparable<x>interf Ace.

Bounds in method parameters

Java method can be parametrized, too. The syntax is as follows:

?
1 <T> T getRandomElement(List<T> list) {}

As with class definitions, you often want-restrict the type parameter in the method. A method which takes a list of Vehicles and returns the fastest Vehicle in the list can has the following type.

?
1 <T extendsVehicle> T getFastest(List<T> list) { }

You can pass as argument a list of any vehicles. List<car> is OK, list<motorcycle> are OK, list<vehicle> is OK, too. List<number> is not OK.

Note that the following declaration wouldn ' t does the trick.

?
1 Vehicle getFastest2(List<Vehicle> list) { }

The argument to the method GetFastest2 have to is exactly a list<vehicle>, and not aList<car >, because list<car> is not a subtype of list<vehicle>,

Wilcards

Take a look at the following declaration.

?
1 <T extendsVehicle> inttotalFuel(List<T> list) { }

The parameter T occurs only once in the method signature, in an argument. You can imagine this method body does not use the name T either. In this case can be use of an alternative syntax, called wildcards, denoted with ?:

?
1 inttotalFuel(List<? extendsVehicle> list) { }

The signatures for Totalfuel is equivalent. The meaning of <? extends vehicle> is:i don ' t care what the type parameter are, as long as it is a subclass of Vehicle.

Introducing Bounds:super

There is also dual bound, called Super. As you guess it's used to denote so can pass only superclasses of the The bound. There is some differences between extends and Super, though.

You can ' t use Super in class declaration

The super bound is not a allowed in class definition.

?
12 //this code does not compile !classForbidden<X superVehicle> { }

Why? Because such construction doesn ' t make sense. For example, your can ' t erase the type parameter with Vehicle because the class Forbidden could is Instan tiated with Object. So you had to erase type parameters to Object anyway. If think about class forbidden<object>, it can take any value in place of X, not only Supercla SSEs of Vehicle. There ' s no point in using Super bound, it wouldn ' t get us anything. Thus it is not allowed.

Wildcards

The syntax for wildcards are also similar to extends:

?
1 inttotalValue(Valuer<? superVehicle> valuer)

The method has to take a comparator which are able to compare VehicleS. If It comparesObjects as well, that ' s fine too.

When to use extends and super

Wildcards is most useful in method parameters. They the necessary flexibility in method interfaces.

People is often confused when to use extends and when to use super bounds. The rule of thumb is the get-put principle. If you get the something from a parametrized container, useextends.

?
1234567 int totalfuel (list<?  extends vehicle> list" { &NBSP;&NBSP;&NBSP;&NBSP; int total =  0 ; &NBSP;&NBSP;&NBSP;&NBSP; for (Vehicle v:list) {           total + = V.getfuel (); &NBSP;&NBSP;&NBSP;&NBSP; } &NBSP;&NBSP;&NBSP;&NBSP; return total;

The method Totalfuel gets Vehicles from the list, asks them on how much fuel they has, and Computes the total.

If you put objects to a parametrized container, use super.

?
1234567 int totalvalue (valuer<?  super vehicle> valuer) { &NBSP;&NBSP;&NBSP;&NBSP; int total =  0 &NBSP;&NBSP;&NBSP;&NBSP; for (Vehicle v:vehicles) {           total + = Valuer.evaluate (v); &NBSP;&NBSP;&NBSP;&NBSP; } &NBSP;&NBSP;&NBSP;&NBSP; return total; }

The method Totalvalue puts Vehicles into the valuer.

It's useful to know that extends bound are much more common than super.

One more tip:if-intimidated by wildcards (which was natural in the beginning), try to write the explicitly paramet rized version first. In typical usage the versions is equivalent. Eventually, you'll figure out when you can get rid of the type parameters and use wildcards.

about how Java generics are used ....

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.