Forums Register Login
How to get instance of child class, not parent class?
I have a child class which extends a parent class.

public class ChildClass extends ParentClass implements Serializable

When I retrieve the child class (see below), I get an instance of the parent class even though the 1st parameter is the child class:
ChildClass childClass = em.find(ChildClass.class, childClassId);

1. I thought I am supposed to get an instance of the child class, not an instance of the parent class. Can someone explain why this is so?
2. How do I get an instance of the child class?


Hi Lin. Welcome to the Ranch!

Why do you think you aren't getting an instance of the child class? Is it throwing a ClassCastException, or is something else going on?
Thanks! Matthew. You're right it is throwing a ClassCastException.

Also, the check below produces the child class:

The child class is an association table with two fields: childID and parentID. The parent class has the parentID as the primary key and about 40 other fields. Modifications to these two entity classes are not allowed.

Crusading Chameleon likes the size of this ad:
ScroogeXHTML 7.1 - RTF to HTML5 / XHTML converter

This thread has been viewed 509 times.

All times above are in ranch (not your local) time.
The current ranch time is
Feb 20, 2018 10:46:49.