How to create a new instance of an extended type

B

Bryan

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!
 
D

Daniel Dyer


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.
 
M

Mike Schilling

Bryan said:
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?

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); }
 
M

martin.rytter

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
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?

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
 
B

Bryan

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top