130 likes | 151 Views
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.
E N D
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.
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.
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(){…}}
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);}
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.
‘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.
TaxedFoodV1 code Public class TaxedFoodV1 extends FoodV1 { private double mTaxRate; public TaxedFoodV1() {} public void setTaxRate(double taxRateIn) {mTaxRate = taxRateIn; } public double getTaxRate() { return mTaxRate; } }
‘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.
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();
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
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();
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.