There is a collection class with the get method, which, depending on the parameters passed, can return an instance of one of the three classes. The only thing in common between classes is that they can all be in this collection.

How from the point of view of architecture it is better to solve this problem?

  1. Inherit all three classes from the same abstract, so that the user himself checks the type and castile.
  2. To do as in StreamTokenizer : get an Enum with three values, and a getType() function that returns this Enum . The user calls getType and, depending on the result, gets the value using one of the three getLast*TypeName* functions.
  3. Own option.

    3 answers 3

    Option 2 is definitely not ice - it's in C style, but not in Java style.

    I would make an option with interfaces, start a common interface, maybe a hierarchy of interfaces. The interface as a higher level of abstraction gives more freedom.

    PS By the way, it is possible for the interface to have a function of type getType() , which will return the desired Enum , then it will simultaneously implement option number 2

      if each class implements different interfaces, then it would be most logical to stuff everything into a Collection <Object> and get it checked for the type of the class (instanceof). If you need to typify a collection (well, you don’t want unnecessary classes to get there) - imitate one common interface (you can only inherit from one class, and you can implement many interfaces). This is suitable if the classes are generally different in structure, have different methods or purposes.

        one could simply use Object as a type for a collection, but I agree with @Barmaley about the hierarchy of interfaces:

         interface Base {} // будет использоватся в качестве типа для коллекции interface A extends Base {} // конкретный тип 1, должен имплементироватся класом 1 interface B extends Base {} // конкретный тип 2, ... interface C extends Base {} // конкретный тип 3, ... 

        after receiving an item from the collection, you can safely bring it to the desired type:

         if (item instanceOf A) { return (A) item; } // для типа 1, для остальных аналогично 

        ps interfaces are usually needed in order to set something in common between several different classes — to create a common “way” to use different implementations. you are attached to specific implementations. I would advise you to describe your task in more detail, maybe you made a mistake when designing and still have the opportunity to resort to a common interface