Getting into a little bit of confusion here when to use generics. I\'ve looked at Java Generics? but still have a few questions.
Say I have:
public
I'd suggest to focus on semantics first:
Providing that you may have a Bmw
and a Toyota
classes implementing the ICar
interface, then make this question: can a Person
change his car or would it be a different person if he does so?
The generics approach will force you to create a new Person
instance if for some reason you need to change the value of the car
attribute from Toyota
to Bmw
in an existent person instance and thus, this new person will be different from the previous one. Of course, you could create the first Person
instance as Person<ICar>
instead of hooking it to a specific car class but, why use generics then?
The distinction isn't always clearcut but here are a few clues:
And finally, a general thought that I found really useful: if you're unsure, don't be afraid to prototype it. Write the code both ways and check which one looks simpler and easier to comprehend. Show it to someone else without any explanations or maybe wait a day or two and then re-read the code yourself. Then throw the other one away. Throwing away code is good.
I'd tend to favor composition (what you're calling dynamic binding), especially in the case you use. A person is not a type of ICar, so using the generics here is kind of weird (to me anyway). I'd use generics as a way of saying "A container for ICar", as in Garage although in that case I might just use a collection type as a variable, or extend the collection type if really needed.
A person is generally not parameterized with a type of car. Only very annoying persons are defined by their car. Persons change cars too (in time). So I would not parameterize the class, if only for the semantics.
Think about what you try to mimic from the real world, before going into such programming details.
This has to do with using Inheritance versus Composition.
Without knowing any other semantics, Composition seems more relevant. A person may change cars, without becoming a different person.
http://www.artima.com/objectsandjava/webuscript/CompoInherit1.html http://en.wikipedia.org/wiki/Composition_over_inheritance
You need the generics version if you have any methods that take or return anything involving a T, or if it's possible for other people to access your car
field. (Since you didn't show any methods, we can't really tell.)
For example, with the generics version you can have a method like T someMethod();
, then when someone has a Person<Honda>
, they know they can get a Honda
back when they call someMethod
, rather than some unknown type of car if you didn't have generics.
Similarly, with the generics version you can have a method like void anotherMethod(T anotherCar);
, then when someone has a Person<Honda>
, this forces them to pass a Honda
to this method, instead of any car.
So basically, having a generic class allows you to place constraints on uses of the object later on (method calls etc.). If the constructor is the only place that you use T, and you don't need to use T in any methods or fields, then yes, there is no point for it.