0

I am familiar with programming, but I am learning Java. Why is it ok in Java to say

A test = new B()

Where A would be a different class from B (obviously). I understand that the new B() portion is just calling the initializer method for the B class. What confuses me is why would you WANT to do that? Is the A test creating the object in computers memory, with the initialization of A?

This question has nothing to do with interfaces. Why this is marked as a duplicate I have no idea...

iOSGuy93892
  • 137
  • 7
  • The reason for `A` being different from `B` is explained in this question: [What does it mean to “program to an interface”?](https://stackoverflow.com/q/383947/5221149) – Andreas Nov 06 '17 at 00:49
  • @Andreas there's more to this question than that. For example, they're asking whether `A test` is what creates the object, which isn't answered under that question. – Chai T. Rex Nov 06 '17 at 00:52
  • Honestly this is why I dont like stack overflow. This guy Andreas did not read my question, but for some reason has the power to mark my question as a duplicate. Honestly, I just think that users have way too much power... – iOSGuy93892 Nov 06 '17 at 00:55

1 Answers1

0
  1. The A test part is declaring that you have a variable called test and its type is A.
  2. The = part means you're setting test on the left to what's on the right.
  3. The new B() part means to create a new object of type B in memory.

So the only part that's creating an object in memory is part 3, new B(). To see this more clearly, you can also do something like this:

A test = existingObject;

In this case, there is no new object created. test is assigned an object that already exists in memory, thus it's the new B() in your example that creates the object.


As far as assigning an object of a different type to a variable, the object has to be a subclass (or implementation) of the variable's type. So, if B is a subclass (or implementation) of A, then A test = new B(); will work fine.

When using the variable test, only the methods defined for class (or interface) A will work for you. You'd have to reassign it to a variable of type B to gain access to B's peculiar methods.

Chai T. Rex
  • 2,972
  • 1
  • 15
  • 33
  • So what is it called when you create a different object from what you are declaring the type as? I was trying to find out more information about this online, but I was having a hard time expressing it in a way that the search engine could find me decent results. I would like to research this more. Also... does declaring the type define the methods that an object gets/what it can inherit? Once again I have a large amount of questions on this, so some references to further reading for this topic would be appreciated! – iOSGuy93892 Nov 06 '17 at 00:58
  • It's just called standard object creation. There's nothing special going on with the object creation itself. The assignment, however, is called *polymorphic assignment*. I've added a bit on how it works. – Chai T. Rex Nov 06 '17 at 01:06
  • I know we arnt supposed to thank people... but seriously thank you! I was stuck on this for an two hours. – iOSGuy93892 Nov 06 '17 at 01:07