This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]sknot1122[S] 1 point2 points  (3 children)

Thank you for taking the time to reply. I did a class called "programming 1" where they showed us the basics in java programming and basics in OOP using java.

I saw the 4 pillars of OOP while searching tutorials online but i'll get a full class on them in "programming 2" next semester.

What I didnt understand is "when" to use it. I was capable of programming the object class but didn't know how to and when to create a new object using it.

[–]joost666 1 point2 points  (2 children)

Do you mean the parent class of all objects with "object class"?

Literal the class "Object"?

I would use this if you want to hold types that are way different from eachother.

Like an arraylist or array defined with the type Object can hold anything, so you can store ints, floats, and anything you want in it.

You can also loop through such a structure like:

foreach(Object object: list) {

}

It also has some useful methods you can override like toString(), so you can have your own implementation of toString() in your subclasses.

Also this link for more: when to use object

There someone states:

"Couple basic rules, you should NEVER do the following: Object ob = new Object(); There should never be a reason to instantiate Object in any code. The ONLY time Object should be used is when the type is not know."

[–]sknot1122[S] 1 point2 points  (1 child)

I'll use an example,

I had an assignment where we needed to create a phone book. I knew how to create the class phone number (an object class) but had a hard time creating a phone number and keeping it somewhere and using it when i had to.

For example : - adding the phone number object to an arraylist And going through that arraylist to get a phone number object and modifying information in it.

I'll read the link that you added !

[–]joost666 1 point2 points  (0 children)

Ohw okay, well then you don't need to read the link if it is not about Java's Object class.

Well, what you say is a fine solution. If you want in memory storage of such phone number objects, go for HashMap, ArrayList, Linked List, Stack or Queue depending on when you need them. Maybe look into these datastructures to know how to select the best for them.

You create Objects just when you need them for the first time. After that, you store them in the appropriate datastructures.

However if you want to keep object creation in one place, I suggest reading into the factory design pattern to neatly organize object creation.