Download presentation
Presentation is loading. Please wait.
1
Java Unit 11: Inheritance I
The Keyword ‘super’, Methods, and Construction
2
Keyword ‘super’ You might have observed in the previous section that in overriding the getPrice() method, the majority of the code simply duplicated the logic of the method being overridden. It was necessary to get the price including the markup and then apply the tax rate to that. The following example shows how it is possible to make use of the method as defined above it in the hierarchy in order to implement the overriding method lower down in the hierarchy:
3
Keyword ‘super’ public double getPrice() { double price; price = super.getPrice(); price = price + price * mTaxRate; return price; }
4
Keyword ‘super’ At execution time the key word super signals that the call to getPrice() is not a recursive call. The system looks in the hierarchy for the nearest class above the current class where getPrice() is implemented, and uses the version found there.
5
Keyword ‘super’ and Constructors
The keyword super also arises when writing subclass constructors which take parameters. The reason that this topic is complex is that constructors in a subclass also do not have direct access to the instance variables inherited from the superclass. In other words, without new syntax it is not possible to write a constructor for the TaxedFoodV1 class which would have parameters for the wholesaleCost and markup and successfully use those parameters to initialize the inherited instance variables.
6
Default Constructor It is possible to write classes with no constructors at all. If this is done, the system automatically provides default constructors which take no parameters. It is also possible to do as done in the example code so far, namely write constructors which take no parameters and contain no code, and thus rely on default initialization. The effect is no different from providing no constructors at all, but it is less cryptic. It is important to remember that as soon as the programmer writes any constructor for a class other than a default constructor, then the system does not provide a default constructor. At that point, if a default constructor is desired, the programmer has to provide it.
7
‘super’ and Constructors
So far, the example has made use of default construction. Default subclass constructors rely on the existence of a default constructor in the superclass. Default initialization of instance variables, including inherited ones, is handled automatically. After construction, it is possible to use set methods to store non-default values in the instance variables, if desired. The example will now be extended to illustrate construction with parameters.
8
‘super’ and Constructors
The modifications are significant enough that the classes in this section will be named with V2 at the end. Example code will be given for various pieces of code for this version. In order to save space, a complete set of code will not be given. In the next section more modifications will be introduced and a complete set of code will be given there.
9
‘super’ and Constructors
Here is a constructor for FoodV2, which takes a full set of parameters for its instance variables: public FoodV2(String nameIn, double wholesaleCostIn, double markupIn) { mName = nameIn; mWholesaleCost = wholesaleCostIn; mMarkup = markupIn; }
10
‘super’ and Constructors
Then this is the correct form for a constructor in the TaxedFoodV2 class below it that takes a full set of parameters, including parameters for the inherited variables: public TaxedFoodV2(String nameIn, double wholesaleCostIn, double markupIn, double taxRateIn) { super(nameIn, wholesaleCostIn, markupIn); taxRate = taxRateIn; }
11
‘super’ and Constructors
When the system encounters a call to super() in a constructor it looks for a constructor above the current class in the hierarchy. It will use the nearest constructor in the hierarchy above it which has a parameter list which matches the parameters used in the call. If you explicitly call super() in a subclass constructor, this call has to be the first line of code. If you don’t explicitly call super(), then you are relying on the default constructor in the superclass.
12
‘super’ and Constructors
Keep in mind that if the superclass contains any other constructor, then it becomes the responsibility of the programmer to provide the needed default constructor. In other words, you will get a compiler error under the following scenario: The superclass contains a constructor that takes parameters, but not an explicit default constructor; the subclass contains a constructor that depends on the existence of a default constructor above it. Typically, such a constructor would be a default constructor. In general, it is a constructor which does not call super().
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.