CSCE
155
Handout 25: In-Class Forum 4:
Superclass vs. Subclass:
This Data Member is Mine!
November 18, 2005
Today, we talked about building a software system that plans how to re-stock the inventories of a grocery store, weekly or monthly. A grocery store has numerous departments and sells numerous items. There are three activities for this forum:
Activity 1: Enter the name of the most appropriate superclass and its data members; and enter the names and unique data members of three additional subclasses.
Activity 2: Decide whether the subclasses should cover all instances of the superclass (i.e., should the superclass be declared abstract?). Explain.
Activity 3: Choose a method that will be implemented differently in the subclasses to utilize polymorphism. And then show how each unique method should be implemented for each of the subclasses.
Here are the results of the forum.
There are five groups, about 5-6 students in each group: (1) Last Class of the Day, (2) Name, (3) JLazy(), (4) Super Saver, and (5) Pick Something.
|
Group |
Superclass |
Subclass1 |
Subclass2 |
Subclass3 |
|
1 |
Noun |
Food |
Accessories |
Chemicals |
|
2 |
Item |
Produce |
Frozen |
Dry |
|
3 |
Grocery |
Produce |
Dairy |
HBA |
|
4 |
Products (should be singular) |
Perishable |
NonPerishable |
NonFood |
|
5 |
Product |
Perishable |
NonPerishable |
NonEdible |
Overall, the superclass is well-named and conceptually good. The classname “Noun” is too generic. For a grocery store, it is probably not appropriate. But from the viewpoint of a software company, that might be appropriate if the class would be re-used for other applications. The classname “Products” is not good – it should be “Product”, singular. As for the subclasses, they are well-named and conceptually good in terms of the level of these subclasses in the inheritance hierarchy. For each group, the subclasses are at the same level. An inconsistent one would be to have “Food”, “Fruit”, and “Apple” for subclasses.
|
Group |
Superclass (Data
members) |
Subclass1 (Data
members) |
Subclass2 (Data members) |
Subclass3 (Data
members) |
|
1 |
Noun (expiration date, project_amount_sold_per_month, location, sku/item#, type/name, size, weight |
Food (how_to_store, temperature) |
Accessories |
Chemicals (hazard_level) |
|
2 |
Item (price, expiration date, aisleNumber, itemName) |
Produce (pricePerPound, weight) |
Frozen (storageTemperature, freezerSectionNumber) |
Dry (storageType) |
|
3 |
Grocery (price, name, qtyOnHand, qtySold, cost) |
Produce (sellBy, produceCode) |
Dairy (sellBy, UPC) |
HBA (UPC) |
|
4 |
Products (should be
singular) (cost, barcodeID, retailPrice, quantity) |
Perishable (expiration date, date received) |
NonPerishable (average shelf time) |
NonFood (hazardous) |
|
5 |
Product (price, size, name, vendor, shelfCode, static – remaining stock) |
Perishable (expiration date, specialStorageReq.) |
NonPerishable |
NonEdible (tax) |
In terms of the data members for the superclass, the appropriate ones are expiration date, price, name, cost, qtyOnHand, qtySold, quantity, and vendor. Remember that the objective of this software system is to plan how to re-stock the inventories for a grocery store. Thus, data members such as “aisleNumber” are not appropriate.
As for the data members for the subclasses, most of the groups come up with 1 or 2 or no unique data members for their subclasses. That is okay. Sometimes, we use inheritance (superclass, and subclasses) not to exploit of the unique data members but the unique method implementations. Group 1’s data members are not appropriate – as they do not pertain directly to the task at hand. Group 3’s data members are appropriate. Group 5’s data members are quite appropriate too although I am not too sure about the usefulness of “specialStoraegReq” for the task at hand.
Overall, all groups are able to build a superclass-subclass hierarchy conceptually and grasp the notion of inheritance well.
This was well-done. Yes, usually, if all the instances are covered by the subclasses, then we should declare the superclass abstract. That’s the case for Group 4 and Group 5. When the subclasses do not cover all the instances, then we should not declare the superclass abstract. That’s the case for Group 1, Group 2, and Group 3.
|
Group |
Superclass (method
implementation) |
Subclass1 (method
implementation) |
Subclass2 (method
implementation) |
Subclass3 (method
implementation) |
|
1 |
abstract Date computeExpirationDate() |
return DateReceived+getShelfLife() |
return null |
return Math.abs(getHalfLife()*getEvaporationDate() – π) |
|
2 |
restock() { defaultMinimumAmount } |
· check Produce state · remove bad produce · check Amount needed – depends on Amount · add Amount needed |
· check for freezer burn · if freezer burn, defrost · add amount needed |
· check expiration · add amount needed |
|
3 |
getNumToOrder() { numToOrder = maxQty – qtyOnHand; return numToOrder; } |
if (inSeason) numToOrder = maxQty-qtyOnHand; else numToOrder = .5*(maxQty-qtyOnHand); |
|
|
|
4 |
abstract void receiveShipment(int quantity) |
setPriority(High) |
setPriority(Median) |
setPriority(Low) |
|
5 |
abstract double calculatePrice(); |
if (expired == true) return price/2; else return price; |
return price; |
return price + price*tax; |
Group 3’s method for subclass1 is appropriate. That highlights the uniqueness of that method for that particular subclass (Produce). Group 5’s method implementations for all three subclasses highlight each method’s uniqueness. For subclass3’s method, since only NonEdible items are taxed, it highlights the uniqueness of the method for that subclass. As for subclass1’s method, this is probably not ethical, especially for Perishable items. 8-) Group 2’s methods are appropriate as well, highlighting the uniqueness of each implementation for the same method.
Overall, students show understanding of what polymorphism is: the same behavior (same method name) but different implementations. From the viewpoint of a client programmer, all he or she needs to know is just one single method name, the same consistent behavior that he or she can expect that all subclasses of a superclass has. That makes coding easier and code maintenance more convenient.