File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Changing the class of an entity at some point of its life Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Changing the class of an entity at some point of its life" Watch "Changing the class of an entity at some point of its life" New topic
Author

Changing the class of an entity at some point of its life

Francesco Pino
Greenhorn

Joined: Apr 27, 2009
Posts: 4
I have the following hierarchy: Party is a base class, extended by Person and Corporation.
I need to change the object class of an entity at some point of its life, and I don't know what's the best way to do this.

I'm modeling the financial world, so I have that a Party can own shares of a Corporation, while only Corporation can have shareholders. Something like this:


I build the objects reading data from a source, and it can happen that at first I know only the name of a party, but I don't know wheter it is a Person or a Corporation.
But I need to create an object, so I create a generic Party. After that, it may happen that I come to know more infos, such that my Party was a Corporation. So, I need to change the class that represents that entity from Party to Corporation.
Until now, my solution was to build a new object, copying the old data into it.
But I'm not satisfied with it, and I'm wondering what's the best way, pattern or whatever else, to implement what I'd like to achieve.

I thought to the State Pattern, but I think it is best suited for other situations.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12788
    
    5
No you can't change the class of an existing object.

Just create a new object with the new identity and replace the old.

Bill

Francesco Pino
Greenhorn

Joined: Apr 27, 2009
Posts: 4
William Brogden wrote:No you can't change the class of an existing object.

Just create a new object with the new identity and replace the old.


You are right, my question wasn't fully correct.
What I meant was how to change the TYPE of an entity during its life.
A way of describing the type of an entity is through the java Class, but I don't think it's the only one. I'm thinking to something like the State Pattern
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
Francesco Pino wrote: . . . What I meant was how to change the TYPE of an entity during its life. . . .
You can't change the type of an object. But remember an object has several types, that of its class, that of java.lang.Object, that of all its other superclasses, that of each interface it implements. So you can cast an object to all those types.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14150
    
  18

Francesco Pino wrote:What I meant was how to change the TYPE of an entity during its life.
A way of describing the type of an entity is through the java Class, but I don't think it's the only one. I'm thinking to something like the State Pattern

The class of an object essentially is the type of an object. As already said, there is no way to change the type (or class) of an existing object. You might be making it too complicated (the State pattern?). As William said, the easiest way to do this is to just create a new object that takes the data of the old object (plus some new data, depending on what's necessary for the new class).

As Campbell says, you can cast from one type to a type higher up in the inheritance hierarchy. But casting doesn't do any kind of conversion, so you can't use casting to convert an object from one class to a completely different, unrelated class.

To make it more concrete: Suppose you have class Animal, and class Dog extends Animal, and class Cat extends Animal. When you have a Dog object, you can cast that to an Animal - no problem, because a Dog is an Animal. But you can ofcourse not cast it to a Cat - because a Dog is not a Cat and cannot be converted into one.

Suppose that at some point in your program you have a Person object, and you find out you need a Corporation object instead. What you'd then do is create a new Corporation object that you initialize with the data from the Person object, and then you throw the Person object away.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12788
    
    5
Having a variable representing the current "state" of an object is done all the time.

Your use of "TYPE" is not the common usage.

Bill
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

// I could have sworn that I posted a response earlier today.

Campbell Ritchie wrote:You can't change the type of an object. But remember an object has several types, that of its class, that of java.lang.Object, that of all its other superclasses, that of each interface it implements. So you can cast an object to all those types.
The term "casting an object" is wrong. Because like you said an object can't change. However you can cast the reference, to the object, to another class/interface. So it should be casting the reference. But no matter what class/interface you're casting the reference to, the same method will be invoked (except for static methods).


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
Yes, you are correct; as soon as you said "cast and object" was wrong, I realised it should be "cast a reference."
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Changing the class of an entity at some point of its life