Hello all,
Say I have the following two objects:
public class ObjectA {
public void load() { }
}
public class ObjectB extends ObjectA { }
ObjectB is a rarely used extension of ObjectA, but does need to be
created every once in a while. What I want to be able to do is load
data into ObjectA, and if certain data criteria is met I want ObjectA
to now be of type ObjectB instead. Is there any way to "perminately
cast" the type of an object? For instance, is there a way to make an
object that was created as ObjectA now be as if it were created as
ObjectB?
Thanks in advance!
Jean-Francois Briere - 20 Oct 2006 22:29 GMT
> For instance, is there a way to make an
> object that was created as ObjectA now be as if it were created as
> ObjectB?
No.
Daniel Dyer - 20 Oct 2006 22:52 GMT
>> For instance, is there a way to make an
>> object that was created as ObjectA now be as if it were created as
>> ObjectB?
>
> No.
But you could use a copy constructor. Add a constructor to class B that
takes an instance of A as an argument and copies all of its fields. Then
you can do this:
ObjectA myA = new ObjectA(some, data);
ObjectB myB = new ObjectB(myA);
It's important to note that this results in a completely distinct object
and is not really like a cast.
Dan.

Signature
Daniel Dyer
http://www.uncommons.org
Mike Schilling - 21 Oct 2006 00:53 GMT
> Hello all,
>
[quoted text clipped - 13 lines]
> object that was created as ObjectA now be as if it were created as
> ObjectB?
No. A better model for this might be:
Interface AorB{ ...}
public class ObjectA implements AorB{
public AorB load() {
// load the data into this.
if (needB())
return new ObjectB(this);
else
return this;
}
private boolean needB() {}
}
public class ObjectB implements AorB{
private Object A a;
ObjectB(ObjectA a)
{
this.a = a;
}
}
This is, have ObjectB add behavior to an ObjectA by wrapping it, and express
their common behavior by interface implementation rather then inheritance.
The only drawback is that you might wind up writing a fair amount of
delegation code in ObjectB, e.g.
foo(args) { return a.foo(args); }
martin.rytter@gmail.com - 21 Oct 2006 09:07 GMT
Hi
> ObjectB is a rarely used extension of ObjectA, but does need to be
> created every once in a while. What I want to be able to do is load
[quoted text clipped - 3 lines]
> object that was created as ObjectA now be as if it were created as
> ObjectB?
You have a few options here:
1) Use the decorator pattern (also known as wrapper). This is basically
the solution Daniel proposed. This is most flexible approach. However,
you will need a fair about of delegation.
2) Use the factory pattern. This patterns seperates the creation from
behavior. See example:
interface MyObject { void foo(); }
class MyObjectA implements MyObject { void foo() { .. some behaviour ..
}
class MyObjectB implements MyObject { void foo() { .. some other
bevaiour .. }
class MyObject C extends MyObject A { void foo() { super.foo(); ...
some more stuff extending existing functionality .. }
class MyObjectFactory()
{
MyObject createMyObject(.. some arguments? ..)
{
create the object
}
}
Google "decorator pattern" and "factory pattern" for more details and
examples.
/mrj
Bryan - 23 Oct 2006 00:36 GMT
Thanks for the suggestions guys! I was considering either the wrapper
or factory pattern, but I wanted to make sure there wasn't an easier
way first.
I'll let you know how it turns out!
Thanks again!
Bryan
> Hi
>
[quoted text clipped - 35 lines]
>
> /mrj