1 / 13

Java Unit 11: Inheritance I

Java Unit 11: Inheritance I. Writing the Code for Subclasses. It is easiest to explain the process of writing subclasses by showing an example. Not all of the concepts of inheritance can be easily illustrated using cups and seeds, so a new class is introduced.

lissettea
Download Presentation

Java Unit 11: Inheritance I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Unit 11: Inheritance I Writing the Code for Subclasses

  2. It is easiest to explain the process of writing subclasses by showing an example. • Not all of the concepts of inheritance can be easily illustrated using cups and seeds, so a new class is introduced.

  3. This new class represents kinds of food items from the point of view of the store where they are sold. • Instances have: • A name • Wholesale cost • Markup • So, an object of this class might have instance variable values “Del Monte 12 oz. canned corn”, 0.99, 0.05. • The name of this class is FoodV1. V1 indicates a version count. • This class doesn’t use the keyword “extends” so it is by default a subclass of the Object class.

  4. FoodV1 code public class FoodV1 { private String mName; private double mWholesaleCost; private double mMarkup; public FoodV1() {} public void setName(String nameIn) {…} public String getName(){…} public void setWholesaleCost(double wholesaleCostIn) {…} public double getWholesaleCost() {…} public void setMarkup(double markupIn) {…} public double getMarkup() {…} public double getPrice(){…}}

  5. FoodV1 code All methods work as you would expect (they get and set their corresponding variables). Except for getPrice(): public double getPrice() { return (wholesaleCost + wholesaleCost * markup);}

  6. It would be possible to create a subclass of this class, which we’ll call TaxedFoodV1. Here is a UML diagram of our classes so far, with TaxedFoodV1.

  7. ‘extends’ keyword • The keyword for specifying a subclass is extends. It is practical to capture the relationship between classes syntactically by having the child specify its one parent, rather than having the parent specifying its potentially many children. • This also explains the direction of the arrows in the diagram. • A subclass “knows” its parent. That is, the superclass is mentioned by name in the code for the subclass. • The parent class does not “know” who its children are.

  8. TaxedFoodV1 code Public class TaxedFoodV1 extends FoodV1 { private double mTaxRate; public TaxedFoodV1() {} public void setTaxRate(double taxRateIn) {mTaxRate = taxRateIn; } public double getTaxRate() { return mTaxRate; } }

  9. ‘extends’ keyword • The first line of the class declaration shows the use of the key word extends in the syntax for making a subclass: • public class TaxedFoodV1 extends FoodV1 • The TaxedFoodV1 class is quite simple. It has one instance variable, a constructor with no parameters, and set and get methods for the instance variable.

  10. Constructing the subclass • What effect does inheritance have on objects of this class? • Construction of an instance of the class in a program in no way differs from the construction of an instance of any other class: • TaxedFoodV1 myTaxedFood = new TaxedFoodV1();

  11. Variables and Methods inherited • What can be said about the instance variables and methods of such an object? The class TaxedFoodV1 inherits all instance variables and methods from the class FoodV1. This means that the object myTaxedFood has the following instance variables in it: • mName: inherited • mWholesaleCost: inherited • mMarkup: inherited • mTaxRate: not inherited

  12. Variables and Methods inherited • It also means that after the construction of the object, calls to methods defined in the superclass, such as the following, can be freely made: • myTaxedFood.setName(“Peas”);double retrievedCost = myTaxedFood.getWholesaleCost();double retrievedPrice = myTaxedFood.getPrice();

  13. Default Constructors • The constructors in both classes rely on default initialization of the instance variables. • This means that strings are null, numerical variables are zero, and booleanvariables are false. • Trying to do otherwise introduces complexities that are best put off until later. • Since there are set methods for all of the instance variables, the objects can be freely used and values assigned to their instance variables even though the classes only include default constructors.

More Related