Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic class with two class hierarchies

I have the following Java generics question

I have the following generic class thay may be sketched as:

public class MyClass<T> {
    AnotherClass<T> another;
    OtherClass<T> other;
    ...
}

where ... represents code that is not relevant to the case.

For the class MyClass<T> is not as important which exact type T is (as of now) but for both:

  • AnotherClass<T>

  • OtherClass<T>

is absolutely crucial what the generic type is and decisions will be made at runtime in base of that.

Based on that, the type T is not completely arbitrary, it may be either an instance of a hierarchy of classes T_1 or a hierarchy of classes T_2.

As is defined the class, the type T is equivalent to Object but I know that is equivalent to either T_1 or T_2

There is not businnes relation between entities T_1 and T_2 therefore I'm not doing:

public interface BaseT { ... }
public class T_1 implements BaseT { ... }
public class T_2 implements BaseT { ... }

public class MyClass<T extends BaseT>

Clarification about why using a generic if they are unrelated:

I'm defining (trying to) a generic class for both because even they are unrelated explictly, there is a implicit relation because both T_1 and T_2 can and will appear associated to the entity represented in MyClass

T will be the same for MyClass, AnotherClass and OtherClass so that in a instance there will only be either T_1 or T_2 but never both at the same time.

My question is, which alternatives do I have here other than design an interface for MyClass and implement it for both T_1 and T_2?.

Can I achieve something like MyClass<T extends T_1 or T_2>?

Kind regards

like image 654
Jorge Lavín Avatar asked Sep 06 '18 07:09

Jorge Lavín


People also ask

Can a generic class have multiple generic parameters?

A Generic class can have muliple type parameters.

Can generics be used with inheritance in several ways what are they?

Generics also provide type safety (ensuring that an operation is being performed on the right type of data before executing that operation). Hierarchical classifications are allowed by Inheritance. Superclass is a class that is inherited. The subclass is a class that does inherit.

Can generic class be inherited?

You cannot inherit a generic type. // class Derived20 : T {}// NO!

Can we define more than one generic datatype in a class?

You can also use more than one type parameter in generics in Java, you just need to pass specify another type parameter in the angle brackets separated by comma.


1 Answers

Probably, this is not exactly what your're looking for, but you might give it a try:

Create an abstract generic class that implements everything:

public abstract class MyClass<T>
{
  AnotherClass<T> another;
  OtherClass<T> other;

  // Add any code needed

}

Then create 2 generic classes for both base classes.
These classes may be empty if all code can be implemented in the abstract one:

public class MyT1Class<T extends T_1> extends MyClass<T>
{
}

public class MyT2Class<T extends T_2> extends MyClass<T>
{
}
like image 119
Robert Kock Avatar answered Oct 22 '22 07:10

Robert Kock