Monday, May 29, 2017
4:43:39 AM
Users online: 0   You are here >> Home > Programming

Forums | Programming Forums search
Forum FAQ
2/7/08 12:59:10 AM
I have to do a BlueJ project for my school, and I'm currently facing a few difficulties.

Firstly, could somebody explain the two relations to me [Inheritance and Usage]? We haven't been taught any of that in school, and I'm thoroughly confused.

Okay, now my main problem.
See, I'm doing this project on a Shop. So, I have this one class for adding products. All of them get stored in an array. Next, I have another class called Inventory. In that, depending on what the user enters in the output window, he can do a variety of things. One of them is viewing the product list. Now, for that, I need to use the array which is there in the first class I mentioned. How do I do that?
Do you people suggest that I use two methods in the same class? Will that allow me to use variables from another method? Or is it something to do with relations that I don't know about?

Your help is really appreciated.

Thank you!


2/7/08 1:33:40 AM

Child Class
Usage (no idea what that actually is >_>)



2/7/08 6:50:58 AM

Inheitance forms an "is a" relationship. In your example, if you inherit a Cup class from a Product class, for your shop, you can say 'Cup "is a" Product'.

Your naming might be a bit off. When dealing with inventory systems, you usually either have Products or Inventory. They are used interchangeably but mean the same thing. If you want a class that controls the sale of Products, you'd be better served to name it something like Store or StoreFront or Counter .

A Store would have a collection of Products. (Stored in an array or other Collection type). That's pretty easy to understand, and consistent with the real world. Store's sell products.

Determining what methods your class should contain is most easily done by thinking about your real world example. Consider the notion of "responsibilities", and what object would be reponsible for a particular action. In the real world, a Store would be responsible for the products it sells, so you should query the Store object for that list.

Store myStore = new Store();

Purchasing products is much the same. In real life you purchase from a store. In your programme you want to pass the Product you want to purchase, to Store object to process the sale. You might want to purchase the "product of the day", for example (Like the daily special at a restaurant?), after you've inspected it and decided you want it.

Customer roh = new Customer();
Product dailySpecial = myStore.getProductOfTheDay();
if (roh.isProductAppropriate(dailySpecial))

In the above code, the product of the day on offer by the store is retrieved (from the store object, as that is the object that would be responsible for knowing the product of the day). The customer inspects the product of the day to make sure it's what is wanted. If it is wanted, the customer purchase the product from store.

Now, you might think "But doesn't the customer do the purchasing?". This is true, but think about what happens when an item is purchased. Who is responsible for decreasing the stock count, taking the money, producing a receipt, etc? The store is the only object that would know about the various business specific operations tht must occur as part of a sale.

Hope that has helped your understanding.

EDIT: Changed capitalisation of first letter of method name to be more Java friendly :)

Edited by kikz: 2/7/2008 6:53:51 AM

Q6600 | 4Gb PC6400 | 2 x 500Gb RAID 0 | 19" Benq FP591 + 24" Samsung 245B + 19" Dell | 8800GTS 640 Mb + 8400GS 256 Mb | Gigabyte GA-P35-DS3P | Antec P182 | Corsair HX-620 | Thermalright 120 Extreme | Vista x64

23/7/08 11:50:40 AM

A class for adding products? A class should represent a particular model, so you should have a class that is Products. Your Inventory class would then implement an array of Products.

<= knight of the 6fAOEC =>

27/7/08 12:18:44 AM

"Inheritance" is "gaining the properties" from a "super class" by(usually) being a class that was created from the super class by
"extension", thus it has the ability to "access for usage" the fields methods and variables of its super class.

The other two more obscure ways of "sub classing" aside to "extension"
are an "internal nested class" constructed inside a class, or a class
created by "type casting" to that class object.

Any subclass "inherits" the methods , class fields (global static) and
variables of its superclass unless they are hidden by access modifier declarations in front of them such as private or protected(public declared infront of a variable will allow it accessible from anywhere).

class Product{
static String product_line="This product type is a"+System.getProperty("line.separator");
Product(){}// constructor
void printDescription(String longdesc){
}// end description method
} // end class

class Cup extends Product{
Static String non_unix_cups=" liquid vessel!";
} // constructor
void printDescription(int serial){
System.out.print("Notation: "+((String)non_unix_cups)+((String)new Integer(serial).toString()));
} // end description method for class Cup
} // end class

note that the field "product_line" inside the class "Cup" is in the template and not passed on to Cup by
a custom code call.
Also the class Cup has become "overloaded" with the method "printDescription" and to prevent confusion now require "super" used to isolate the class "Product"s' own printDescription method or it would use its own [akin to: this.printDescriotion(.......)].

Edited by nicephotog-jvm.net: 27/7/2008 12:33:12 AM

NIN - Reptile (Reciprocating Marine engine)

27/7/08 12:39:24 AM

Quote by nicephotog-jvm.net
Also the class Cup has become "overloaded" with the method "printDescription" and to prevent confusion now require "super" used to isolate the class "Product"s' own printDescription method or it would use its own [akin to: this.printDescriotion(.......)].

nicephoto, you mean overriden :)

RoH, you wouldn't go implementing classes for each type of product. ;) In stock control, the type of product doesn't have differing behaviour. No more differnt than if you went and created a kikz class and a roh class to represent the different behaviours of people if you were representing a membership system.

Edited by kikz: 27/7/2008 12:41:59 AM

Q6600 | 4Gb PC6400 | 2 x 500Gb RAID 0 + 2 x 320 Gb RAID 0 | 19" Benq FP591 + 24" Samsung 245B + 19" Dell | 8800GTS 640 Mb + 8400GS 256 Mb | Gigabyte GA-P35-DS3P | Antec P182 | Corsair HX-620 | Thermalright 120 Extreme | Vista x64

29/7/08 2:45:21 PM

Kikz: "nicephoto, you mean overriden :)"
No for the point only), "overridden" is when a method is replaced (usualy abstract) by rewriting the exact same call signatures template at the level it was created(but that does match when you come to think of it).
"overloaded" is when a method signature is "the same name" in the same class, hence printDescription() has different arguments but the same signature name.
With "super" i think that one comes to "a combination of both" since "it is possible with super" to have the same signature name AND the same argument types and arrangement exactly.
Thereabout either would do i suppose.

String[] productList;

productList = new String[10]; //10 products
for(int i=10;i<productList.length;i++){


String[] productList={"produceA","produceB","produceC"}; // literal array

Edited by nicephotog-jvm.net: 29/7/2008 02:49:21 PM

NIN - Reptile (Reciprocating Marine engine)

Forums | Programming