0

Is it possible to change the object type in runtime using reflection?

Let's say I have an instance of an object A. Objects of type B extend A. Would it be possible to change the object type of the instance of A into B so I can safely make a cast of A into B?

Maybe by changing the final attribute .class or a similar trick?

1
  • 1
    No, the type of an object can never change. Commented Sep 16, 2014 at 15:09

1 Answer 1

3

No, it is not possible. Reflection that you mentioned allows discovering given class at runtime (i.e. finding method, field etc by name) and accessing methods and fields of objects at runtime without compiling the client code against specific class. For example you can invoke method foo() of any class.

Changing type of object actually does not make sense for me at all. Object contains is an instance of speicific class that have both data and methods. I can somehow imagine way to change the memory allocated for object at runtime using sun.misc.Unsafe, however I even cannot imagine how can you change the implementation of methods done in specific class associated with the object.

And the question that still remains here: why? Could you probably explain your task and ask for solution proposal?

EDIT

Following the new information posted by OP as a comment to my answer I'd like to add the following.

As far as I understand the situation is the following.

There is a third party library that implements class A and AFactory. OP uses code like the following:

A a = AFactory.create();

However he does not need A. He needs B extends A that implements additional functionality.

Possible solution is the following.

Create class B extends A:

public class B extends A {
    private final A a;
    public B(A a) {this.a = a;}
    // delegate all methods of A, i.e.:
    @Override
    public boolean isA() {return a.isA()}

    // add your functionality, e.g.
    public boolean isC() {/* your code here*/}
}

Now use this class as following:

A a = AFactory.create();
B b = new B(a); 

Now your can use all functionality of A via B and the additional functionality as well.

Sign up to request clarification or add additional context in comments.

5 Comments

I get an object from a factory in a 3rd party jar and now I need to extend it. These object have methods like: isA, isB... to distinguish the type of object... Now I need to extend this object and the only solution I see is adding an extra method isC in my new extending class. I dislike this approach of isA, isB, isC and I was thinking that maybe I could change the class of this object to be a A class , B class or C class.
You cannot extend object. You can extend class. Please see additions to my answer in a couple of minutes.
@danielsp, please read additions to my answer and let me know if they help.
Hi Alex. Thank you very much for your help here. The point is that my extending class would have exactly the same functionality of the extended class. Actually an object returning isA true or isB true are exactly the same except for the attribute that tells if this object is A or is B. Now I just need a new object with a different value in this attribute which would represent, if I follow this 'model', having a new method isC. This all would be unnecessary if I could change the class of these objects to be A, B, C or D if needed in the future.
Forget about changing the class at runtime. You can do it using byte code engineering (e.g. using ASM), but IMHO it is overkill for you. I showed you usage of decorator pattern for your case. You can prefer just to inherit the class without wrapping it and implement a kind of copy constructor. In this case you do not have to implement delegating methods.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.