Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Unit 11: Inheritance I

Similar presentations


Presentation on theme: "Java Unit 11: Inheritance I"— Presentation transcript:

1 Java Unit 11: Inheritance I
Access to Instance Variables and Overriding Methods

2 Inherited Variables are Not Directly Accessible
Both instance variables and methods are inherited. In a program that uses an instance of a subclass, it is possible to work with the object’s instance variables by using the corresponding get and set methods, which are also inherited from the superclass. In any code written in the subclass itself it is necessary to use the inherited get and set methods to access inherited instance variables. These inherited variables are not directly accessible in the subclass because they are declared private in the superclass.

3 By definition, subclasses are more detailed in nature than their superclasses.
They may include new, additional instance variables and methods. As subclasses become more specific, it is possible that an inherited method does not completely accomplish what needs to be accomplished.

4 Method Overriding We have seen that within a single class it is possible to have more than one method with the same name, and these methods are differentiated by their parameter lists. This is referred to as overloading. It is also possible for a subclass to have a method with the same name as a method in one of its superclasses, and with the same parameter list. This is referred to as overriding the inherited method.

5 Method Overriding If a method has been overridden in a subclass, if a call is made on an object of the subclass using that method name, the version of the method defined in the subclass is used, not the version in the superclass. In other words, in this case inheritance is thwarted. It is also possible to write a method in a subclass with the same name as a method in one of its superclasses, but with a different parameter list. In this case you have not overridden the inherited method.

6 Method Overriding The list of parameters used in the call determines whether the version of the method defined in the superclass is used, or the version defined in the subclass. Note the possibility for confusion between overloading and overriding. When overloading, you have different methods with the same name, which are distinguished by their parameter lists. When overriding, you have different methods with the same name and the same parameter list, which are distinguished by their location in an inheritance hierarchy.

7 Parent Class Child Class Method Overriding myMethod() [version 1]
myMethod(int x) [version 1] myThing() [version 1] Child Class myMethod() [version 2] myThing(int x) [version 1] Inherited: myMethod(int x) myThing() Overridden: myMethod() Overloaded: myThing(int x)

8 Method Overriding and Hidden Variables
Here is a specific example that illustrates both overriding a method, and the fact that within a subclass you do not get direct access to variables inherited from the superclass. The method getPrice() as defined in the class FoodV1 calculates a checkout price based on the wholesale cost of the item and the markup charged by the store. However, the checkout price for a taxed food item should include the tax on that item.

9 Method Overriding and Hidden Variables
The following is not Java code; it is pseudo-code that illustrates the general calculation necessary to find the price of an item after both the markup and the tax are included: costPlusMarkup = wholesaleCost + wholesaleCost * markup; price = costPlusMarkup + costPlusMarkup * taxRate;

10 TaxedFoodV1 getPrice() Override
public double getPrice() { double retrievedCost; double retrievedMarkup; double costPlusMarkup; double price; retrievedCost = getWholesaleCost(); retrievedMarkup = getMarkup(); costPlusMarkup = retrievedCost + retrievedCost * retrievedMarkup; price = costPlusMarkup + costPlusMarkup * mTaxRate; return price; }

11 Method Overriding and Hidden Variables
Note these lines of code in the method implementation: retrievedCost = getWholesaleCost(); retrievedMarkup = getMarkup(); In case the meaning is clearer, recall that they could be written in this way: retrievedCost = this.getWholesaleCost(); retrievedMarkup = this.getMarkup();

12 Method Overriding and Hidden Variables
These methods are declared public in the superclass, so they can be freely called within the code of a subclass. The important points about the example are the following: 1) The implicit parameter of the call to getPrice(), a TaxedFoodV1 object, would have wholesaleCost and markup variables by inheritance. However, they can only be accessed through inherited get methods. 2) Sometimes it is desirable to write an implementation of a specific method in a subclass which differs from the implementation that would otherwise be inherited. In this example, the getPrice() method of FoodV1 is overridden in the subclass.

13 Method Hierarchy Another question arises concerning the inheritance and overriding of methods. Consider the following general inheritance hierarchy:

14 Method Hierarchy If methodX() is implemented in the Top class and not implemented in the other two classes, then it is inherited by both Middle and Bottom. If methodX() is implemented in Middle, then it does not exist for objects of the Top class. Inheritance does not go upwards. However, the method is inherited by the Bottom class. Suppose that methodX() is implemented in the Top class, overridden in the Middle class, and inherited by the Bottom class. Which version is inherited?

15 Method Hierarchy The answer is, the version nearest to the Bottom class as you move upwards through the hierarchy. In other words, in this case the version implemented in the Middle class is inherited. Inheritance only goes downwards. When the system is looking for methods that are inherited, it looks upwards and stops looking after finding the nearest occurrence higher up in the hierarchy.

16 Instance VariableS Not Overridable
A final question arises concerning overriding. It is clear that inherited methods can be overridden. Is it possible to somehow override instance variables? The answer is simply “No.” It is syntactically possible to declare instance variables in subclasses with the same name as instance variables in its superclasses. However, no overriding occurs. You simply have different variables with the same name defined in different places. It is a mistake to do something like this. Not only will it be extremely difficult to unravel the code; the real reason that such an approach is wrong is conceptual.

17 Instance Variables Not Overridable
The idea behind inheritance is that subclasses are of the same kind as superclasses. Methods can be overridden, but the instance variables should represent inherent characteristics of classes. If it seems desirable to change an instance variable in a subclass, that’s a sign that that class is not truly a subclass of the superclass it has been placed under in the hierarchy.


Download ppt "Java Unit 11: Inheritance I"

Similar presentations


Ads by Google