unc comp 110 fall 07.

Java "class" and inheritance recitation


This recitation is an introduction to creating your own Java classes. The context will be products in a store similar to products in the program 4 online store. Products would include, food, soaps, cleaning supplies, clothe, electronics, toys, etc.

Why an example about store products? Using products is helpful for demonstrating inheritance. The concept of inheritance enables making relationships between similar classes. Specifically, a sub-class inherits the data and methods from the super-class. In our example the Product class is a generic product (the super-class). Other products (sub-classes), such as produce and member discount products, are similar to the generic product. These products have a name and price. The purpose of sub-classes, however, is to model the details that differ from the generic product. In our example the cost of all three kinds of products varies.   

Parts:
  1. Create the Product class
  2. Using Product class in a main program
  3. Product class. public/private
  4. Produce sub-class
  5. Member sub-class
Solution