Promoting an object

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi, I browsed for a related question/answer without luck, so here it goes:

Is there a known pattern that would allow me to promote an object of class A
to subclass B which inherits from A?
In other words, how can I turn a point into a circle or a dog into a
dalmatian? I know I can do the other way around with casting.

Thanks in advance.
 
PJChan said:
Hi, I browsed for a related question/answer without luck, so here it goes:

Is there a known pattern that would allow me to promote an object of class A
to subclass B which inherits from A?

Well, the object itself can never change type.
In other words, how can I turn a point into a circle or a dog into a
dalmatian? I know I can do the other way around with casting.

The normal way would be to provide a constructor in the derived class
which takes a reference to an instance of the base class, and creates a
new object based on it.
 
PJ,

Provided that Dalmatian inherits from Dog, and the declarations:

Dog dog;
Dalmatian dm;

Stating

dog = dm;

is OK, but the other way around you must use:

dm = (Dalmatian) dog;

or

dm = dog as Dalmatian;

Regards - Octavio
 
Thanks Jon,
the part of "creates a new object based on it" do you mean that it keeps a
private instance of the object or that somehow the constructor "returns" a
new instance of the subclass B based on the passed object (which is of class
A)?

I resolved the requirement temporarly using the former method, but would
really prefer to use the later. How do I construct the new instance? Some
form of copy or clone? Would you mind providing a simple example?

thks
Pedro
 
Thanks Octavio
following your second example I always get a Casting exception or a null (if
I use the "... as ...")

My understanding is that under C# you cannot typecast from class to subclass
just like that as private members would not necessarily be initialised
properly.

I don't mind using a construct like: objB = new SubClassB(objA)
but I don't know how to go about creating the new instance based on the
existing objA, and I don't know if it is possible that objB and objA share
everything that exists in class A but objB has the extensions supplied by
subclass B.

Thanks
Pedro
 
PJChan said:
the part of "creates a new object based on it" do you mean that it keeps a
private instance of the object or that somehow the constructor "returns" a
new instance of the subclass B based on the passed object (which is of class
A)?

That's up to the implementation. If everything's done via properties,
it could be either - although you'd want to think of the ramifications
of what it meant to change the value of a property of the new object,
for instance.
I resolved the requirement temporarly using the former method, but would
really prefer to use the later. How do I construct the new instance? Some
form of copy or clone? Would you mind providing a simple example?

It really depends on what access the derived class has to the base
class. If the derived class has access to a sort of "copy constructor"
of the base class which just copies the values of all the fields from
one object into the new one, that would make things easier. Otherwise,
you may need to set the values of properties.

In general, it's something I try to avoid needing - does your design
absolutely depend on it?
 

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

Back
Top