-7

For example

Dictionary webster = new Dictionary();

what does each dictionary mean technically? I understand that I'm creating a object that has its methods and variables. But what causes this?

Furthermore I am learning about Data structures.. Same exact question really but can the left and right side be different? Ex

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

I know

ArrayList<String list = new ArrayList<String>(); works

but like I said I'm confused on what each side brings when my object is created..

twokdavey
  • 37
  • 6
  • 1
    See http://stackoverflow.com/questions/3686647/whats-the-best-way-to-define-the-words-class-and-object-to-someone-who-hasn ... http://stackoverflow.com/questions/19211215/what-happens-when-you-create-a-new-object ... long story short: did you try **any** prior research? – GhostCat Oct 05 '16 at 05:09
  • 1
    Learn the basic of Object Oriented Programming and its features like Inheritance and Polymorphism. – Prabin Upreti Oct 05 '16 at 05:14
  • @ghostcat those had zero help for me. I mean the left side Dictionary, what does that mean, compared to the right side. – twokdavey Oct 05 '16 at 05:17
  • You define a some entity is of a certain type. Like saying "Child baby = my newborn niece". But again: this is not programming school where you get **taught** such super basic things. – GhostCat Oct 05 '16 at 05:20
  • Can someone else explain. I know how to create objects from classes. I just don't know why we call Dictionary twice.... School didn't teach this – twokdavey Oct 05 '16 at 05:25
  • @twokdavey Hi, seems you are stuck at very basic level and the topic being very broad, i suggest you to go through some java basics material. You can refer Head First Java, First 5 chapters will align you. – vv88 Oct 05 '16 at 05:46
  • @vv88 thank you for your kind guidance, I figured it was called decoupling after I talked to a old friend about it. – twokdavey Oct 05 '16 at 06:04

1 Answers1

-1

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.

Sweeper
  • 213,210
  • 22
  • 193
  • 313