Workshop: Lab 9 Engineering Software 1 Lab 9: Classes and Objects: Inheritance Learning Objectives In this lab exercise you will learn: • How inheritance promotes software reusability. • The notions of superclasses and subclasses. • To use keyword extends to create a class that inherits attributes and behaviors from another class. • To use access modifier protected to give subclass methods access to superclass members. • To access superclass members with super . • How constructors are used in inheritance hierarchies. • The methods of class Object , the direct or indirect superclass of all classes in Java. Lab Work General Note: Study every exercise and for each of them draw a structure diagram. Type in all these programs and run them inside your Java development kit. In your log-book take note of all the results from the execution of these programs and write down any problems that you had to solve. For each program of the exercises, explain clearly what each section of the program is doing. You may need to perform a walk-through, line by line, where this is appropriate. Record all your explanations in the log-book. Remember to put a date in your log-book and also to offer some discussions and a brief conclusion after every week’s work. Finally, complete the programming assignments at the end of the exercises, then enter comments in your logbook as you are doing this and save to your floppy disk/CD for submission with your logbook. Background • Software reuse reduces program-development time. • The direct superclass of a subclass (specified by the keyword extends in the first line of a class declaration) is the superclass from which the subclass inherits. An indirect superclass of a subclass is two or more levels up the class hierarchy from that subclass. • In single inheritance, a class is derived from one direct superclass. In multiple inheritance, a class is derived from more than one direct superclass. Java does not support multiple inheritance. • A subclass is more specific than its superclass and represents a smaller group of objects. • Every object of a subclass is also an object of that class’s superclass. However, a superclass object is not an object of its class’s subclasses. Stavros Dimitriou ©London South Bank University 1 Workshop: Lab 9 Engineering Software 1 • An “is-a” relationship represents inheritance. In an “is-a” relationship, an object of a subclass also can be treated as an object of its superclass. • A “has-a” relationship represents composition. In a “has-a” relationship, a class object contains references to objects of other classes. • A subclass cannot access or inherit the private members of its superclass–allowing this would violate the encapsulation of the superclass. A subclass can, however, inherit the non- private members of its superclass. • A superclass method can be overridden in a subclass to declare an appropriate implementation for the subclass. • Single-inheritance relationships form tree-like hierarchical structures–a superclass exists in a hierarchical relationship with its subclasses. • A superclass’s public members are accessible wherever the program has a reference to an object of that superclass or one of its subclasses. • A superclass’s private members are accessible only within the declaration of that superclass. • A superclass’s protected members have an intermediate level of protection between public and private access. They can be accessed by members of the superclass, by members of its subclasses and by members of other classes in the same package. • The first task of any subclass constructor is to call its direct superclass’s constructor, either explicitly or implicitly, to ensure that the instance variables inherited from the superclass are initialized properly. • A subclass can explicitly invoke a constructor of its superclass by using the superclass constructor call syntax–keyword super, followed by a set of parentheses containing the superclass constructor arguments. • When a subclass method overrides a superclass method, the superclass method can be accessed from the subclass if the superclass method name is preceded by the keyword super and a dot (.) separator. • Declaring instance variables private, while providing non-private methods to manipulate and perform validation, helps enforce good software engineering. • Method toString takes no arguments and returns a String. The Object class’s toString method is normally overridden by a subclass. • When an object is output using the %s format specifier, the object’s toString method is called implicitly to obtain its string representation. Stavros Dimitriou ©London South Bank University 2 Workshop: Lab 9 Engineering Software 1 Part A: Reinforce your understanding of key Java programming concepts Exercise 1: After reading the background above along with lecture notes, answer the given questions. The questions are intended to test and reinforce your understanding of key concepts. For each term in the left column, write the letter for the description from the right column that best matches the term. Exercise 2: Answer the following questions in your logbook. Your answers should be as concise as possible; aim for two or three sentences. 1. Why would a class provide overloaded constructors? 2. How does inheritance promote software reusability? 3. Explain protected member access. 4. Explain the difference between composition (i.e., the has-a relationship) and inheritance (i.e., the is-a relationship). 5. Explain how to invoke a superclass method from a subclass method for the case in which the subclass method overrides a superclass method and the case in which the subclass method does not override a superclass method. Stavros Dimitriou ©London South Bank University 3 Workshop: Lab 9 Engineering Software 1 Part B: Java programs Determine if there is an error in each of the following program segments. If there is an error, specify whether it is a logic error or a compilation error, circle the error in the program and write the corrected code in the space provided after each problem. If the code does not contain an error, write “no error.” [ Note : There may be more than one error in a program segment.] For exercises 1–3 assume the definitions of classes CommissionEmployee4 and BasePlusCommissionEmployee5 in Fig. L 9.1 and Fig. L 9.2. Stavros Dimitriou ©London South Bank University 4 Workshop: Lab 9 Engineering Software 1 Stavros Dimitriou ©London South Bank University 5 Workshop: Lab 9 Engineering Software 1 Exercise 1: The following constructor, when inserted into class BasePlusCommissionEmployee5, should invoke a CommissionEmployee4 constructor to initialize the CommissionEmployee4 part of a BasePlusCommissionEmployee5 object. Stavros Dimitriou ©London South Bank University 6 Workshop: Lab 9 Engineering Software 1 Exercise 2: The following toString method, when inserted into class BasePlusCommissionEmployee5, should return a string consisting of CommissionEmployee4’s output string along with the base salary of this object. Exercise 3: The following earnings method, when inserted into class BasePlusCommissionEmployee5, should use CommissionEmployee4’s earnings method to help calculate the earnings for this object. Stavros Dimitriou ©London South Bank University 7