Re: Casting a parent class to a child class
- From: "S. Lorétan" <tynril@[nospam]gmail.com>
- Date: Wed, 11 Oct 2006 11:49:52 +0200
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.
.
- Follow-Ups:
- Re: Casting a parent class to a child class
- From: Peter Duniho
- Re: Casting a parent class to a child class
- References:
- Casting a parent class to a child class
- From: S. Lorétan
- Re: Casting a parent class to a child class
- From: Chris Dunaway
- Casting a parent class to a child class
- Prev by Date: Re: OOT: USB Port Dongle
- Next by Date: Re: How to use a huge memory in C#?
- Previous by thread: Re: Casting a parent class to a child class
- Next by thread: Re: Casting a parent class to a child class
- Index(es):
Relevant Pages
|