I want to be sure, I asked a Generic class and I write an example, But after I post here one friend told me using of generic is pointless in your example!
If that is the case then no harm no foul. It helps if you are more specific with the wording of your question IMO. As to whether or not it is pointless .... interfaces are used to provide a unified type and define behavior. If you don't intend to use this interface anywhere else other than a specific class, then it could be deemed as pointless.
If we are assessing whether it is pointless to include a generic type in your interface ... then your friend is incorrect. You are creating a definition of behavior that can be enforced and checked at compile-time while allowing the flexibility of passing in generic types .. I don't see how that functionality is pointless.
To elaborate, I may want to utilize your interface to add people to my Classroom class, or maybe Teachers to my Faculty class and maintain the Classroom and Faculty instances referenced by the interface you defined. A bit contrived but a very common place theme as the problem size scales.
That looks like a completely different question from what you asked at first. The idea behind your generic method (with the <E> in its heading) is that using methods can "tell" the method that they are going to use a particular type of array. You are right that you have given a poor example., but that is because you are using its toString() method, called implicitly by %s and printf. Because Object has a toString method, every possible object has a toString method. Let's alter that method of yoursYou will find the Engine interface here. Now you know you are restricting your array to contain Engines only, you can be confident in calling the start() stop() and useFuel() methods on everything in your array. You can pass Engine arrays, or PetrolEngine arrays, or Dieselengine arrays or ElectricEngine arrays.
And will you succeed? Yes you will indeed! (98 and 3/4 % guaranteed) - Seuss. tiny ad: