Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
namespace CSharp_Practicing
{

    class Program
    {

        static void Main(string[] args)
        {
            Animal a = new Dog(); // THE QUESTION IS ABOUT THIS VARIABLE !!!
        }
    }

    class Animal { }
    class Dog : Animal
    {
        public void SayBau()
        {
            Console.WriteLine("Baaaaaau");
        }
    }
    class Cat : Animal
    {
        public void SayMaw()
        {
            Console.WriteLine("Mawwwwwwwwwwwww");
        }
    }
}


The variable a in compile-time is of type Animal and in run-time its of type Dog ? Am I right or I am understanding it wrongly ?

What I have tried:

Asking question here in CodeProject.com
Posted
Updated 20-Jul-17 23:27pm
v2

Animal a = new Dog(); 


You're creating two things, you're creating an object (Dog) and a reference variable (a). The object is a Dog, it will always be a Dog and part of being a Dog means also being an Animal. The variable you create is of type Animal and is pointing to the Animal part of the Dog object. As the object "a" points to is a Dog you could do this

Dog d = (Dog)a;


You still only have one object (Dog), however you have created another reference variable (d) that points to the Dog part of the object. So now you have one object (Dog) and two reference variables that point to this object; "a" which is looking at the Animal part and can only access Animal properties and methods, and "d" which is looking at the Dog part and can access any property or method on the Dog part or the Animal part (as Dog inherits Animal, Dog is considered an Animal too).

Now consider this

Cat c = (Cat)a;


Will this compile? Yes. The compiler only knows that "a" must be an animal, so it is *possible* that it might be a Cat but it doesn't know at compile time so it gives you the benefit of the doubt. However at run-time it will throw an error as there is no "Cat" part of the object that "a" points to, as "a" points to a Dog.
 
Share this answer
 
v2
Comments
Ralf Meier 21-Jul-17 5:33am    
Well explained ... +5
Roughly speaking, yes.
You may use it in every context a Animal is allowed (hence its 'compile-time' type), however its actual type, at runtime, is, polymorphically, Dog.

Note, your class hierarchy doesn't take advantage of polymorphism.
 
Share this answer
 
Comments
The_Unknown_Member 21-Jul-17 5:10am    
Okay could you explain me this please:
object o = 5;
object y = 6;
object oy = o + y;
Console.WriteLine(oy.GetType());

This is still polymorphishm. Because every class in .NET inherits from the System.Object so i can assign to variable of type System.Object an integer value and the type of this variable in run-time will be integer and in compile-time it will be System.Object. I know that the System.Object has no operator overload "+" for integer numbers but shouldn't it perform o + y in runtime and assign it back to the variable oy ?

Edit: I think I understood this. This is because after the assignment operator of oy I am adding two variables of type System.Object (which are holding integers but in compile-time they are still System.Object) and since System.Object does not support arithmetic operations for integers it cant perform the task in runtime but if i make it object oy = 5 + 6; then it will can because 5 and 6 are integer literals and are not holded by System.Object variable. Did I understand it right ?
It's always a Dog, as you created it that way in your new statement. In reality the variable is not anything at compile time since it does not exist until the program is run.
 
Share this answer
 
Comments
Ralf Meier 21-Jul-17 4:53am    
I don't agree with you ...
Animal is the Base-Type. If you instance a as Animal with Dog you could do this (because Dog derives from Animal) but what you get is only that, what is common - that means you don't have the SayBau-Method ...
Richard MacCutchan 21-Jul-17 5:06am    
You are correct (sort of). Although a is declared as an Animal type, it still refers to a Dog, from the new statement. So you can cast it up to a Dog and get the SayBau method. If it had been created as new Animal you would not be able to do that.
Ralf Meier 21-Jul-17 5:25am    
That's what I meant ... ;)
Richard MacCutchan 21-Jul-17 5:45am    
Uh, so now you do agree with my original comments?
Ralf Meier 21-Jul-17 5:49am    
Yes and No ... but with your additional comments ... because (for me) the enquirer was not sure with what happens with/when derive from something ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900