title: Dynamic Inheritance - Java
body: <p>Dynamic Inheritance</p>
  
  <p>I have a situation where I want a class (which is a JPA entity) to be able to extend either class A or class B dynamically. My thought was to use Generics, but it looks like generics doesn’t support this. For example:</p>
  
  <pre><code>
  @Entity
  public abstract class Resource() {
  ...
  }
  
  @Entity
  public abstract class Snapshot() {
  ...
  }
  
  public abstract class CommonRS&#60;R extends Resource, S extends Snapshot&#62; {
  /* This is the class that I want to dynamically assign Inheritance to. */
  ...
  }
  
  @Entity
  public class FeedResource extends CommonRS&#60;Resource&#62; {
  ...
  }
  
  @Entity
  public class FeedSnapshot extends CommonRS&#60;Snapshot&#62; {
  ...
  }
  </code></pre>
  
  <p>The reason I want to do this is that FeedResource must Inherit from Resource and FeedSnapshot must inherit from Snapshot because both classes are using the JPA join strategy <code>InheritanceType.JOINED</code> and are persisted to different tables, however they both share common attributes and I would like them to be able to inherit those common attributes. </p>
  
  <p>I understand that I can I could use @Embeddable on CommonRS and the Embed it in both FeedResource and FeedSnapshot.</p>
  
  <p>Since Java doesn’t support multiple inheritance, I can’t see any other way to do this other than using Embeddable.</p>
  
  <p>Thanks in advance.</p>