My question is, when compiler compiles the code and when the memory allocation begins, we've got two objects right?
Yes.
But one is type A, one is type B, ...
No!!
Both are type B. The expression new B(...) creates a B. What happens after that doesn't change that.
In the first example, you are then assigning the reference for a B instance to a variable whose type is A. That means that you will only be able to use A features (methods, fields) when you access the object via that variable.
However, the object itself is still an instance of B, and will remain that way for the lifetime of the object. And we can prove it1.
System.out.println(typeInterface.getClass().getName());
will print "B", not "A".
And we can go a step further by casting typeInterface to a B and using the B methods and fields ... to show that it is really a B.
It is a B. Unequivocally.
... that means 'typeInterface' will have only one method, but 'typeClass' will contain one more field and one more method.
No. Not true. This logic is based on a false assumption. See above.
Does these two objects allocate the same amount of memory or 'typeInterface' basically consume much less memory?
Yes they user the same amount of memory. They are both B instances. See above.
One way to understand this is that when you do the assignment in this:
A typeInterface = new B();
the compiler "forgets" about the B-ness of the object that typeInterface (now) refers to. It only "remembers" that it refers to an A of some kind. However, at runtime, the runtime system always knows what an object's real type is, so that it can correctly implement instanceof, type-casts, getClass(), method dispatching and so on.
1 - The javadoc for Object::getClass() states: "Returns the runtime class of this Object".