Which is the reason why the generic type parameters make no sense here they don’t change anything.Īs a rule of thumb, type parameters on methods are useful when you want to declare a relationship between two or more parameters or between parameter(s) and the return type. This method will do exactly the same as with the method signature public int computeDistance(MeasurableDistance a, MeasurableDistance b) There is no real reason to use generics there.īut if you were also retrieving the value, generics would be useful: Holder holder = new Holder() įirst of all, when you define a method like public Holder.add(new Person()) // compile time error In case 2, generics let you limit the type of object that can be held by Holder: Holder holder = new Holder() But this is because they don't have a (good) reason to use generics in the first place. The cases that you have shown don't actually have a reason to use bounded generics, like you have pointed out. Let's say that I have two classes that implement and interface (naive examples): public interface MeasurableDistance For example: is an interface for an ADT, but and are actually ADTs, because their actual behaviour does conform the ADT concept.I'm wondering what is the difference between using an interface (or a supertype) in a generic class or method and using the generic approach with bounding (T extends Interface). This means, that to be considered as ADT, your type must carry implementation, which results either in abstract class or a normal class. Its external behaviour must conform the ADT concept. Or create a interface SomeInterface and have 2 common methods as default methods with implementation and let class A and B implement the interface which is the better design I feel implementing interface is preferred than extending a class ( so class A and class B can still extend some xyz class in future if I go for interface now).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |