Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically extend object to some inheriting class

Let's suppose I've got following declarations:

public class ClassA
{
  public string FieldA = "Something first";
}

public class ClassB : ClassA
{
  public string FieldB = "Something second";
}

As we all know object of ClassB can be used in every place where ClassA is required. For example a method

public void DoSthWithObject(ClassA a);

can be called like this:

ClassB b = new ClassB();
DoSthWithObject(b);

It is also possible to cast inherited type as parent type:

ClassA a = (b as ClassA);

So compiler/framework know how to "use" data of ClassB where ClassA is required.

And now the question: is it possible to somehow do the other way round? Let's suppose I've got a variable of type ClassA and I want to start using it as ClassB but at the same time keep all fields that ClassB inherits from ClassA and do not instantiate a new object? I understand that it would require extending memory allocated for object of ClassA to accomodate fields of ClassB.

Simply speaking I want to do something like this:

ClassA a = new ClassA();
ClassB b = (a as ClassB); // some magic happens here

(I know I could implement IConvertible and use Convert or provide some cloning mechanism but I wanted to avoid such solutions.)

EDIT

As it seems that answers are concentrating on proposing alternative solutions or warning me about even thinking to do as I want, I decided to slightly change my question and propose a bounty so maybe I could get better answers.

First of all I want this to be language agnostic.

Second: I really do understand, that not every tool is a hammer, and I can (I hope) distinguish Jack from Jane. I also do not need a lesson on fundamental OOP (unless somebody can actually prove I am making a fundamental conceptual mistake which I am obviously not aware of at the moment). What I want to know is if and why is an automated parent-to-subclass conversion logically impossible and if it is possible (and as it seems from comments there are languages where it is possible) then why it is dangerous/full of flaws. It is also very interesing how is it actually techincally done in these languages, that "supports" it (or at least do not forbid it).

But I do expect real programming examples and not mere metaphors.

Just to make it clear: I am not looking for information on downcasting a previously upcasted object. This is not the case here.

If I am a guy who expects to simultaneously aggregate and group by on the same table column - show me this please. I know I am smart enought to understand. :-) I just do not want to leave this question with a feeling that there is something fundamental about OOP that I do not catch.

NOTICE

In comment under David's answer I was mentioning floats but actually referring to real numbers (mathematical term). It's due to my rather imperfect English. Please stop picking on it. :]

THANK YOU

I'd like to thank everybody for their answers. I decide to give a bounty to Steve, but it doesn't mean that I consider the question closed. I am still looking for arguments against automated object conversion. I must admin that I feel a little dissapointed that those who were warning me most could not provide clear examples related to conversion (and not related to casting).

From my own research I decided to note out a few things in a separate answer (in case somebody may find them usefull). Please feel free to edit and extend the list.

like image 650
Kuba Wyrostek Avatar asked Feb 20 '23 07:02

Kuba Wyrostek


1 Answers

No! From the fundamental description of the classes, ClassA is not a ClassB. The relationship you describe is not symmetric/reflexive. Think of it this way: Every hammer is a tool, but not every tool is a hammer. If you need to turn a screw, you need a tool, but a hammer won't do the job. Replace the class definitions above with that analogy, and I think it becomes fairly clear on its face why the relationship doesn't work.

like image 131
David W Avatar answered Mar 11 '23 16:03

David W