Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the parent base class object super.getClass()

Tags:

I have a little problem with Java (being a C++ programmer).

I have 2 related classes:

public class Patient() { ... }  public class PatientPersistent extends Patient { ...     public void foo() {     System.out.println(super.getClass().toString());     } } 

This will output:

class org.example.smartgwt.server.model.PatientPersistent

Is there a way to get the parent class type? i.e.

class org.example.smartgwt.server.model.Patient.

This will allow me to generalize some methods which I need to implement in each child which is awful.

Thanks!


UPDATE

I'm using Dozer to convert my domain Hibernate object to a Serializable version. I don't want the client to know of this, so the client only sees the Patient class. On the server side I perform conversions.

public class DataObject<Type> {      private static final Class<Object> DstType = Type;      public Object convert(Object srcData, final BeanFactory factory) {         Mapper mapper = (Mapper)factory.getBean("dozerMapper");          return (Object)mapper.map(srcData, DstType);     } }  public class Patient() implements Serializable {     public Set foo; }      public class PatientPersistent extends Patient {      public org.hibernate.collection.PersistentSet foo;     DataObject<Patient> converter = new DataObject<Patient>;      public Patient convertToSerializable(final BeanFactory factory) {         return (Patient)converter.convert(this, factory);     } }  public class main() {     // This object is not serializable so I cannot send it to the client     PatientPersistent serializableBar = new PatientPersistent();      // Using Dozer to copy the data PatientPersistent -> Patient     // This will load the Dozer spring bean and copy as mapped     Patient copiedSerializableData = serializableBar.convertToPersistent(bar, factory); } 

I know this code does not work, but it's just to make my point. I would like to be able to convert the object to it's serializable form so that I can send it back to the client. That's why I would like to give the parent's type. Calling the mapper will always be the same thing, a source object and a Dest.class.

Maybe I'm just too confused with java.

Thanks

like image 456
code-gijoe Avatar asked Jul 20 '10 21:07

code-gijoe


People also ask

What does getClass () do in Java?

getClass() method returns the runtime class of an object. That Class object is the object that is locked by static synchronized methods of the represented class.

How do I find parent class names?

The parent() method is used to return all ancestor of the selected elements. Check if ancestor (parent) class exist then it returns the class name otherwise returns not exist.

What is the base super parent class of Java?

1) super is used to refer immediate parent class instance variable. We can use super keyword to access the data member or field of parent class. It is used if parent class and child class have same fields. In the above example, Animal and Dog both classes have a common property color.


2 Answers

getClass().getSuperclass() 

But don't use this. It is certainly a sign of bad design.

like image 58
Bozho Avatar answered Sep 17 '22 19:09

Bozho


Nice... super.getClass() is actually Object's getClass(), which returns the runtime type of the instance it is invoked on (this in this case). Therefore you receive the same class...

Instead of asking for the runtime class of this using the super's implementation, You should ask for the super class of the class returned by getClass:

getClass().getSuperclass() 

And by the way, what do you mean by "This will allow me to generalize some methods which I need to implement in each child which is awful."? Are you sure you have no other design choice?

like image 29
Eyal Schneider Avatar answered Sep 19 '22 19:09

Eyal Schneider