• Post Reply Bookmark Topic Watch Topic
  • New Topic

Interface, Abstract Classes, and Loading Array  RSS feed

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to Java, and last week had an assignment to create a shopping list. I made it so that I have one class use a ProductData class to load an array of objects (description, price, priority). This week I need to take that program and change it so that it includes an Interface and Abstract Class. I need to also split one class up into at least 2 others.

I am having trouble getting my thoughts together and figuring out what to put in the interface and what to put in the abstract class. I'm thinking that it might be best to split up the ProductData class up into 3 different classes: description, price, and priority. Then have an interface with a print method. Each of those 3 classes will implement the interface.

As for the abstract class, have the price and priority extend the abstract class. The abstract class will be at the same level as the interface and contain the set and get methods. Right now they are of 2 different data types: int, double. Should I make both of them Double, and then use a method to change the priority to an int?

Should price and priority inherit from description, or should they all be at the same level? I am thinking that they should be at the same level because they all describe the item in the array.

My most confusing part is that I have no clue at all on how I can load that array when each object is split up in a different class. My professor went over ArayLists last week, and we can now use them if we want, but the assignment doesn't explicitly say that we should change it to an Array List. Where does the constructor for the ProductData() go? Do I split it up into 3 different constructors?

I'm not sure if you need to see the code to understand what I am talking about. If so, just let me know and I'll post it.
 
Sheriff
Posts: 11496
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

On the surface I might say that this sounds like a discussion that would fit better in the Design forum rather than Beginning Java. However, some things you said hinted at some gaps in basic understanding of OO concepts. For example, with this:
Should price and priority inherit from description, or should they all be at the same level?

it seems you're a little confused about inheritance. Objects can inherit attributes and behaviors (methods) from another object but you don't normally think about an attribute like price or priority inheriting from another attribute like description.

...it might be best to split up the ProductData class up into 3 different classes: description, price, and priority

I doubt description, price, and priority have enough interesting behavior associated with them to warrant creating separate classes. These seem like they are merely attributes of something and that something would be what ... ???

And yes, you will need to post some code to give us a more concrete idea of what you're trying to do. Please UseCodeTags(←click) if you decide to post code.

As far as your design goes, I suggest you start with a few high-level concepts and lay out their main responsibilities first. Then determine what other objects they will work with (their collaborators). Then you need to understand the purpose of introducing an interface and an abstract class and where that fits in. Interfaces and abstract classes are mechanisms for abstraction so you have to ask what behavior you're trying to abstract away or generalize.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!