More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members of the superclass? Answer: for data members of the superclass, also. NOTE: They can be accessed directly by the subclass only if they have the scope modifier protected in the super class. 2. DEF: protected is the scope modifier that permits access to a data field in a superclass by a subclass but no other class. 3. Can the same identifier be used for a local variable in a method, a data member in a subclass, and a data member in a superclass? Answer: yes a. Which will be accessed from within the method? Answer: the local variable b. How can the subclass data member be accessed then from within that method? Answer: this.identifier c. How can the superclass data member be accessed? Answer: super.identifier 4. Can an object of a subclass be referenced by a variable declared to be a reference to the superclass? Answer: yes NOTE: when this happens only methods with names in the superclass identical to those in the subclass can be used BUT it will be the subclass method that is executed!! Casting can be used to improve this. See ex. 6.4 p.384 5. Can an object of a superclass be referenced by a variable that was declared to be a reference to the subclass? Answer: NO (see ex. 6.2 p.385) Casting can be used to do this in some cases: see p.385 6. Can a subclass variable reference an object of another subclass of the same superclass on the same level? Answer: NO 7. What does it mean when we say primitives are passed to parameters in method headings by value? Answer: a copy of the value in the argument (that is in the method call) is made in the parameter (that’s in the method heading). Therefore, any changes made to the parameter do not change the argument (the original data). 8. Since objects are always passed by reference, that is, the parameter receives the address of the object only, changes to the parameter do change the original object. 9. RULE: An argument of a subclass type can be passed to a parameter which is its superclass, BUT not the other way around (that is the converse is false). CAUTION: It can appear that this is not the case when methods in subclasses and superclasses have the same name. Examples: 1. equals() 2. toString() see p.718, 720, 722 for classes: Object & String Fig. 6.16 p.386 shows an equals() method defined in the class NewEmployee, a subclass of the class Object, receiving an instance of the class Object as a parameter! (emp) This will only work if emp had previously received the address of an object of the NewEmployee class. (see ex. P.381) In Fig 6.16 checking with the operator instanceOf and type casting, allows this method to work, thereby, overriding the equals() method in class Object. A call such as clerk.equals(obj) is not breaking the above rule. It is calling the equals() method in class Object (the superclass), which can be done by any subclass. Why is clerk not calling the equals() method in the subclass? (answer: different argument!) 10. Review: Arrays must contain only 1 type component. EXCEPTION: An array of objects may contain subclass objects also, since a superclass variable can reference objects of its own type OR objects of its subclass types. Ex: employees [ i ] . toString() See NewCompany class p. 390. NOTE: With a method such as toString( ) whose name is the same in different classes, Java cannot determine at compile time which toString( ) mthod to call. It is not known at that time whether employee[i] references an object of the superclass or one of its subclasses. However, at run-time, the Java interpreter determines this based on the address then stored in employee[i]. 11. Def: dynamic binding - the programming language feature that permits selection of a code fragment at execution time. NOTE: This is another example of polymorphism. What are other? method overloading method overriding 12. Def: abstract class - a class that is never instantiated. It has the purpose of being a superclass (parent) of a group of classes. It is a place to store data fields and methods common to all subclasses. NOTE: an abstract class must contain at least 1 abstract method (its header only) which must be implemented by each subclass. See p. 406 - abstract class: GeoFigure hierarchy top p.408 - abstract class code p. 412 - test class showing array containing different subclasses. 13. Does Java support multiple inheritance? No. (a class can extend only one superclass.) However, Java provides a mechanism called an interface. An interface is similar to a class but the only members of an interface are abstract methods and final class data fields. (See p.766 ActionListener) A class implements an interface (not extend it). Such classes are guaranteed to have certain functionality (behavior) and constants. Often used in GUI and graphic programs 14. What is a GUI? A Graphical User Interface is a collection of instances (objects) of certain classes called components. Examples of components are: buttons, check boxes, text fields and labels. Each is able to access data fields and methods defined somewhere in a hierarchy. Each handles an action performed by a user called an event which is usually a click of a mouse. Note: packages that include GUI’s are AWT (Abstract Window Toolkit) and Swing (which an improved alternate to AWT). See example DistanceGUI p.448 and method actionPerformed() p. 458. On website “boxProjects” see boxApplet.java 15. Java provides a collection of graphics methods that enable you to draw pictures on a drawing surface (computer screen). The class Applet has an associated drawing surface accessed through its paint() method. This method requires one argument which is an object of the Graphics class defined in the AWT package. Therefore, we must import both packages. See ex. Intersection p.159, House p.162 drawBox (website).