File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Methods and Objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Methods and Objects" Watch "Methods and Objects" New topic

Methods and Objects

Marc Kryzhan

Joined: Mar 17, 2004
Posts: 14
public InventoryItemInterface getInventoryItem(String itemName);
I have the above script in an assignment.
Firstly, What does InventoryItemInterface represent here? My guess, An Object? Is getInventoryItem an instance of this Object?
The other methods I have are in this following format (which I understand):
public void deleteInventory(String itemName);
I do have an interface called InventoryItemInterface but no methods in it called getInventoryItem().
My second part of this query is what capabilities does getInventoryItem() have if it is an object of InventoryItemInterface? Does it have the capability of using the methods in InventoryItemInterface?

[ March 19, 2004: Message edited by: Marc Kryzhan ]

Newbee to java.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
The line you had in bold declares a method. If it's in a class file, the method body will immediately follow this declaration. But with that semi-colon on the end, it looks like this line was found in an interface file.
Either way, the syntax of this declaration tells us
1) the method is public
2) the method will return an object of type InventoryItemInterface
3) the method requires an argument of type String
We would expect this method to create the object it's going to return or maybe find it in a cache or get it from some other object. The choices are endless, but let's take the simplest choice and create the new object on the spot.
Because we see Interface in the name of the return type, we can guess the return type is an interface and not a "concrete" class. That's a cool thing. The method promises to return an object that implements the interface, but doesn't tell you exactly what class that might be. This gives our method the freedom to create different objects depending on the itemName argument.
One confusing bit - with just that one line to go on, we don't have any idea what class actually implements this method. So who you call to get an inventory item is simply not known.
I'm resisting providing code samples of how this could work, hoping you'll take a shot at them yourself. Can you take a guess at what you'd see in the method body? At how somebody would call this method?
BTW: This is supposed to be fun! Don't worry that somebody here will jump on you if you guess wrong.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Marc Kryzhan

Joined: Mar 17, 2004
Posts: 14
You are right the declared method exists in an interface file. I've been struggling with this one for a few hours now. Ugh!
I've commented out all the methods in StoreInterface except for three, two of which I have working. I'm implementing the methods in Store.
In this particular instance
public hw9.interfaces.InventoryItemInterface getInventoryItem(String itemName) {
// inventory.indexOf(itemName);
InventoryItem name = new InventoryItem();
return name.getName();
I'm using NetBeans to compile and am currently bombing on the return statement. The methods setName and getName are simple methods found in ItemInventoryInterface.
I could use some help with some coding examples. Pleeease! The above setName and getName methods aren't what I really need but I'm first trying to take baby steps with the app and get something to return. I'm supposed to prompt a user for an itemName and then display details about the item. I have a collection that is storing objects (name, type, price, cost).
Anyhow to get to C I first need to understand B.
Thanks ahead of time.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Your example looks like you're getting the idea.

The line where I added "uh oh" probably doesn't compile. Your method declaration (which should exactly match the one in the interface) promises to return something that implements InventoryItemInterface. I'd bet that the InventoryItem you just created does, so you probably want to return that.
I'm a real nut on names. I'll change the name of a variable several times before I get it right. Eclipse is very nice about helping with that kind of thing. Anyhow, maybe you could do better than "name" for a variable name.

You had a couple lines commented out that look like you're checking to see if the item is already in the inventory. indexOf() makes me think you're testing a Vector. That's probably not your best choice here. If you find inventory items by name, you might want to look at the Map interface, and the HashMap implementation. Hey, another chance to look at interfaces in action! This kind of logic is very common with maps:

But now "new" is not such a good part of item name. See why I change them a few times before they come out right?
Hope that helps. Keep us posted!
[ March 21, 2004: Message edited by: Stan James ]
Marc Kryzhan

Joined: Mar 17, 2004
Posts: 14
I ended up getting the gist of types and iterators through this forum.
My end code:
public InventoryItemInterface getInventoryItem(String itemName) {
InventoryItem inv = null;
int invSize = inventory.size();
System.out.println("Store::getInventoryItem() Size Of Inventory Is: " + invSize);
if (inventory.isEmpty()) {
allocator = "empty";
return inv;
else {
Iterator i = inventory.iterator();

while (i.hasNext()) {
inv = (InventoryItem);
if (itemName.equals( inv.getName() )) {
System.out.println("Found Information For Item: " + inv.getName());
allocator = "found";
return inv;
allocator = "Not Found";
return inv;
Thanks for your help. Marc
[ March 29, 2004: Message edited by: Marc Kryzhan ]
I agree. Here's the link:
subject: Methods and Objects
Similar Threads
JSON array to Java object
I am lost... Help me please
Problem with Web Service Response
collection exercize
Accessing objects from Collection