I have had a burning question for a long time now, relating to the Java syntax and why it allows this:
Object object1 = new Integer();
Why can we declare child classes using Object as a reference? Also, let's take a look at this piece of code:
public class Parent{}
public class Child extends Parent{}
public static void main(String[] args){
Parent var1 = new Child();
Object var2 = new Child();
Object var3 = new Parent();
}
All three cases will run, as I have tried this. The question is, does it matter whether I use Object to reference a new object/variable or the actual class name? Also, how does the reference type selected affect the program and code? Does a certain reference type like "Object" take up less memory or make the program more efficient? Thanks in advance