[Solved] Creating an object from a class [closed]


what does each dictionary mean technically?

The first Dictionary indicates that the type of the variable is Dictionary. Since you’re creating a new dictionary, you call the Dictionary class’ constructor i.e. Dictionary(). You can think of the new keyword as a word that you need to write in order to call any constructor.

That said, these are two valid statements (although they don’t do anything practical):

Dictionary dict; // declares a variable of type Dictionary but doesn't give it a value
new Dictionary(); // creates a new dictionary but doesn't put it into some variable

Same exact question really but can the left and right side be different?

This is called polymorphism. As I said, the first Dictionary represents the type of the variable, so AbstractList<String> here:

AbstractList<String> list = new LinkedList<String>();

represents the type of the variable list as well.

However, a variable of type AbstractList not only can store AbstractList objects. It can also store objects that are compatible with AbstractList. In this case, it is storing a LinkedList<String>.

You might ask why LinkedList compatible with AbstractList. Because the former inherits from the latter!

To sum up,

AbstractList<String> list = new LinkedList<String>();

The left side says:

I have a variable that can only store AbstractList<String> objects and objects that are compatible with it.

The right side says:

I’m creating a new LinkedList<String> by calling its constructor.

solved Creating an object from a class [closed]