Your question's code snippet should be Class1 class1 = new class2();
not class class1 = new class2();
as you can't assign any object to reference of class
.
And This is not method overriding.
Method Overriding is something where you write a method in your class with the same name, same parameters and same return type as one of the methods in its super class.
In your case, it's called Type Casting or Auto boxing/ unboxing. Suppose in your case Class2 extends Class1
then when you will instantiate Class2 actually in memory there will be three objects created in the memory as per shown in the image below -

Now when you'll do Class1 class1 = new class2();
actually reference of Class1 itself will be assigned. But according to the code you'll have a feeling of assigning object of Class2 in Class1 which is impossible in java.
As a thumb rule, you can assign to a reference only the object of that class or null, nothing else.
Now for accessing methods and variables -
- Priorities will always be given to methods of subclass. So if you have 2 methods with same name, same parameters then method of sub class will be invoked. This is called method overriding.
- Instance variables will always be accessed of the same class.