Let Dog be a class inherited from Animal class, we can do the instantiation as follows:
Animal a = new Dog();
I know that at compile time, the compiler only knows that we are declaring a as an Animal, and when it's at runtime, we are pointing a to a Dog object. But there is something vague to me at this point:
Is the compile-time type Animal being the type of the variable a on the stack? Or is it just an information telling the compiler at compile time what type of object may be referenced by a and has nothing to do with the type of the variable a on the stack?
I am asking this because I'm wondering if the information on the compile-time type Animal still exists at runtime or if it will become completely irrelevant and is discarded? If it still exists, is it stored as the type of the variable a on the stack? And if so, when we are referring to the type of a, how does the program know if we are referring to the type of the object referenced by a or the type of the variable a on the stack?
I'm new to this concept and may have some misunderstandings. Could anyone please clarify for me? Thanks in advance!