I
Ikke
Hi everybody,
Suppose I have a class 'Dog' defined as follows:
public class Dog : Animal
Now suppose I have the following code:
Dog fido = null;
Animal animal = new Animal();
Then how do I assign the animal Object to fido?
If I were to write:
fido = (Animal) animal;
I get an error in Visual Studio and the project does not compile.
If I write:
fido = (Dog) animal;
then the code compiles, but I get a runtime exception (invalid class cast).
What I'm looking for, is a way to get the following:
public class Dog
{
public Dog(Animal animal)
{
// super(animal);
}
}
The super(animal) call is invalid, of course, but is there a way to
initialize the new Dog Object to contain the known values of animal?
Thanks in advance,
Ikke
Suppose I have a class 'Dog' defined as follows:
public class Dog : Animal
Now suppose I have the following code:
Dog fido = null;
Animal animal = new Animal();
Then how do I assign the animal Object to fido?
If I were to write:
fido = (Animal) animal;
I get an error in Visual Studio and the project does not compile.
If I write:
fido = (Dog) animal;
then the code compiles, but I get a runtime exception (invalid class cast).
What I'm looking for, is a way to get the following:
public class Dog
{
public Dog(Animal animal)
{
// super(animal);
}
}
The super(animal) call is invalid, of course, but is there a way to
initialize the new Dog Object to contain the known values of animal?
Thanks in advance,
Ikke