Dog dogObj = new Dog();
Yes I know that we have to give the suitable type to the reference variable so it can reserve a place for the object's location address or whatever we put in it.
The reference variable stores a 8 byte address(number) of the real object located in the heap of register. So it's job is just to hold the address of that location. Why does it need a type then? What changes on this 8 byte after giving it a type. It still could point with it 8 byte data to that location even if it has no type.
What is the details behind this?
If you want a variable which can just hold any reference value, that's easy to do:
Object dogObj = new Dog();
However, if you then try to call:
dogObj.bark();
... you shouldn't be surprised that the compiler doesn't know what you're talking about. The compiler uses the declared type of the variable to know how it can be used... it's as simple as that.
See more on this question at Stackoverflow