0

I have a situation where I am creating a data model class that has all the data points needed for a specific class. However depending on what is called in the class it does not need all the variables. The data model has multiple bounded type parameters however if not all are being used can some of them be optional?

For example:

public class DataModel<OBJ extends Object, EXCEPT extends Exception, MODEL extends BaseModelClass> {
}

Then when I instantiate it I might not need model and want to do something like:

DataModel<ClassA,RunTimeException,null> data = new DataModel<ClassA,RunTimeException,null>();

Where ClassA is a defined class that extends object in another part of the code and BaseModel is a company base model that has some very common pieces.

So the question is can something like this be done and have some of the bounded type parameters that apply to fields not being used for a specific submethod in this class be optional?

Patrick Aquilone
  • 584
  • 2
  • 11
  • 28
  • How could you not need `MODEL`? Your class declaration uses it in some `extends`, `implements`, inner class, field, or method declaration, otherwise why have the type parameter at all? The type **must** be specified. – Andreas Apr 18 '18 at 18:23
  • @Andreas The reason is that the class that will use this data model does a variety of things with exceptions (some times creating one some times acting upon a thrown one). One of them is to basically do logging and when logging model is not needed. All we really need is the exception and some business extras that are variables. Whereas when I am creating an exception formatted according to our business standards, I need the model and the exception (in this case the exception to create). So the utility class does share functionality but has different specific needs in the methods. – Patrick Aquilone Apr 18 '18 at 18:26
  • If you truly don't intend to use the parts of the class where the `MODEL` type parameter is used, you could e.g. use the upper bound (`BaseModelClass`), or any other sub-type thereof. You could even create a dummy subclass that with a `private` constructor, so it is uninstantiatable, e.g. `NoBaseModelClass`. --- However, if you need to use the `DataModel` class without all parts of it, it's a sign that your class might be a [God object](https://en.wikipedia.org/wiki/God_object). – Andreas Apr 18 '18 at 18:26
  • 1
    Try `DataModel data = new DataModel<>();` – lexicore Apr 18 '18 at 18:26
  • @lexicore I tried that and it didn't work. Won't compile. Thanks – Patrick Aquilone Apr 18 '18 at 18:29
  • @PatrickAquilone This works, see the example [here](https://ideone.com/56Ogrs). Post your code as [MCVE](https://stackoverflow.com/help/mcve). – lexicore Apr 18 '18 at 18:33

1 Answers1

1

You cannot specify only some of the parameterized types of the generic class as you declare a generic variable : it would be a compilation error. Either all parameterized types of the generic class are specified and valid according to the bounds defined or a raw type should be used.

Note that you could declare the same type as the lowerbound wildcard if you don't want to define a more specific type to:

DataModel<ClassA,RunTimeException,BaseModelClass> data = new DataModel<>();

Or a wildcard such as (as mentioned by Lexicore) :

DataModel<ClassA,RunTimeException,?> data = new DataModel<>();

According to your requirement, I would add that the generics are not designed to add/remove new methods to a class. These are designed to type more precisely instances of the class. Besides, more you define generics in a class used by client classes more it may be cumbersome to use for clients.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • I think `?` in `DataModel data = new DataModel<>();` better communicates the idea "I don't care about this type here" (compared to `DataModel`). – lexicore Apr 18 '18 at 18:33