
Inheritance in Java: Accessing Instance Variables and Overriding Methods
Learn how inheritance works in Java, where inherited variables are not directly accessible in subclasses but can be accessed using get and set methods. Discover how method overriding allows subclasses to redefine methods from superclasses for more specific functionality.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Access to Instance Variables and Overriding Methods JAVA UNIT 11: INHERITANCE I
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.
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.
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.
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.
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.
METHOD OVERRIDING Parent ClassmyMethod() [version 1] Child ClassmyMethod() [version 2] Overloaded: myThing(int x) myMethod(int x) [version 1] myThing() [version 1] myThing(int x) [version 1] Inherited: myMethod(int x) myThing() Overridden: myMethod()
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 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. FoodV1
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;
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; }
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();
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 object, would have wholesaleCost wholesaleCost and markup 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 of FoodV1 FoodV1 is overridden in the subclass. getPrice() (), a TaxedFoodV1 TaxedFoodV1 markup variables by getPrice() () method
METHOD HIERARCHY Another question arises concerning the inheritance and overriding of methods. Consider the following general inheritance hierarchy:
METHOD HIERARCHY If methodX methodX() () is implemented in the Top implemented in the other two classes, then it is inherited by both Middle Middle and Bottom Bottom. If methodX methodX() () is implemented in Middle for objects of the Top Top class. Inheritance does not go upwards. However, the method is inherited by the Bottom Suppose that methodX methodX() () is implemented in the Top overridden in the Middle Middle class, and inherited by the Bottom class. Which version is inherited? Top class and not Middle, then it does not exist Bottom class. Top class, Bottom
METHOD HIERARCHY The answer is, the version nearest to the Bottom move upwards through the hierarchy. In other words, in this case the version implemented in the Middle 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. Bottom class as you Middle class is inherited.
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.
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.