Re: Casting a parent class to a child class



Ok. But I don't understand why it is designed this way.

For me, there is data loss during the cast from a Cat to an Animal. But
there isn't during the (impossible) cast from an Animal to a Cat, because
Cat inherit all the attributes of Animal *plus* some cat-specific ones.

I feel much more natural with this way. I understand that there can't be
both casting (Child to Superclass and Superclass to Child), but the
Child->Superclass cast as C# implements feels strange to my logic.

Let me explain myself.

Animal test = new Cat(); //Valid because a Cat IS an Animal
Dog test2 = (Dog)test; //Whoa, can't change a Cat into a
Dog!!

I don't think it this way. On the line 1, we cast a Cat to an Animal. So,
the Cat object *lose* its specific attributes. Then, on the line 2, we cast
an Animal (with *only* the Animal's attributes) to a Dog, which is valid,
because the Dog class implements every attributes of the Animal class
because it's a child class.

Here is the way I'm thinking about:
Animal an = new Animal(); // We create an Animal with basics attributes
setting to their default value in the constructor...
Dog dog = (Dog)an; // And we add some Dog-specific attributes to this
object while casting it to a Dog.

In this way, there is no data-loss. We can't cast a Dog to an Animal,
because not Animals are Dogs. But we can cast an Animal to a Dog, because
every Dog are Animals!

I don't know if I am understandable, I'm not very comfortable with the
english language. Sorry. By the way, still your point of view is the OOP
way-to-think, it's probably because it is better than mine. I just can't see
where or why. Can you maybe explain me what's wrong in my reasoning?

Thanks and have a nice day!

"Chris Dunaway" <dunawayc@xxxxxxxxx> wrote in:
1160407159.880614.145450@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
S. Lorétan wrote:
namespace Test {
class A {
public string Label1;
}

class B : A {
public string Label2;
}

class Program {
static void Main(string[] args) {
A test = new A();
B test2 = (B)test;
}
}
}


Let's use different names for your classes and see if that clears up
the question. (I added the Cat class to help clarify)

namespace Test {
class Animal {
public string Label1;
}

class Dog : Animal {
public string Label2;
}

class Cat : Animal {
public string Label3;
}

class Program {
static void Main(string[] args) {
Animal test = new Cat(); //Valid because a Cat IS an Animal
Dog test2 = (Dog)test; //Whoa, can't change a Cat into a
Dog!!
}
}
}

The reason it won't cast is becuase while all Dogs are Animals, not all
Animals are Dogs! The test variable in my example is declared as
Animal, but is actually a Cat! A rule of thumb would be that you can
cast a descendent type back to one of it's ancestors, but you can't
cast an ancestor type into a specific descendent.


.



Relevant Pages

  • Re: Casting a parent class to a child class
    ... a Cat to an Animal. ... When you cast it back to the original type, your view changes and you get to ... those things that Cats share with all other Animals. ... along with new default Dog parts. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Casting a parent class to a child class
    ... I think I've now understood what a cast is ... from a Cat to an Animal. ... those things that Cats share with all other Animals. ... somehow populated with the Animal parts, along with new default Dog parts. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: OT ~ The Dog, The Cat, and The Rat! Mortal Enemies?
    ... them rigged a harness up for his cat so she wouldn't have to walk so ... much (like the dog and himself). ... At some juncture the rat came along, ... Like the Mayor, the animals are drugged. ...
    (rec.outdoors.rv-travel)
  • Re: Ellens puppy sob story
    ... |>>dog, while some teenagers are uninterested in the family dog. ... I adopted a cat about nine years ago, picking an adult that was to be ... It also said that if the shelter ... these shelters complain about all the unplaced animals and their need ...
    (rec.arts.tv)
  • Re: Ricin man: a wannabe animal rights terrorist?
    ... I remember one time he put a cat trap out in our field, ... have a lengthy history involving pets and animals. ... but the dog was so starved and parched it had to ... was the room originally tested for ricin, even though none was found at that ...
    (alt.true-crime)