Reserved. 1 Inheritance A form of software reuse in which a new class is created by absorbing an existing class’s members and embellishing them with new or modified capabilities. Can save time during program development by basing new classes on existing proven and debugged high-quality software. Increases the likelihood that a system will be implemented and maintained effectively.
Education, Inc. All Rights Reserved. 2 Figure 9.1 lists several simple examples of superclasses and subclasses Superclasses tend to be “more general” and subclasses “more specific.” Because every subclass object is an object of its superclass, and one superclass can have many subclasses, the set of objects represented by a superclass is typically larger than the set of objects represented by any of its subclasses.
Create classes by composition of existing classes.
Example: Given the classes Employee, BirthDate and TelephoneNumber, it’s improper to say that an Employee is a BirthDate or that an Employee is a TelephoneNumber. However, an Employee has a BirthDate, and an Employee has a TelephoneNumber.
Education, Inc. All Rights Reserved. 9 Objects of all classes that extend a common superclass can be treated as objects of that superclass. Commonality expressed in the members of the superclass. Inheritance issue A subclass can inherit methods that it does not need or should not have. Even when a superclass method is appropriate for a subclass, that subclass often needs a customized version of the method. The subclass can override (redefine) the superclass method with an appropriate implementation.
Education, Inc. All Rights Reserved. 10 A class’s public members are accessible wherever the program has a reference to an object of that class or one of its subclasses. A class’s private members are accessible only within the class itself. protected access is an intermediate level of access between public and private. A superclass’s protected members can be accessed by members of that superclass, by members of its subclasses and by members of other classes in the same package protected members also have package access. All public and protected superclass members retain their original access modifier when they become members of the subclass.
Education, Inc. All Rights Reserved. 11 A superclass’s private members are hidden in its subclasses They can be accessed only through the public or protected methods inherited from the superclass Subclass methods can refer to public and protected members inherited from the superclass simply by using the member names. When a subclass method overrides an inherited superclass method, the superclass method can be accessed from the subclass by preceding the superclass method name with keyword super and a dot (.) separator.
Education, Inc. All Rights Reserved. 12 Inheritance hierarchy containing types of employees in a company’s payroll application Commission employees are paid a percentage of their
sales Base-salaried commission employees receive a base
Education, Inc. All Rights Reserved. 13 Class CommissionEmployee (Fig. 9.4) extends class Object (from package java.lang). CommissionEmployee inherits Object’s methods. If you don’t explicitly specify which class a new class extends, the class extends Object implicitly.
Education, Inc. All Rights Reserved. 14 Constructors are not inherited. The first task of a subclass constructor is to call its
direct superclass’s constructor explicitly or implicitly
Ensures that the instance variables inherited from the superclass are initialized properly. If the code does not include an explicit call to the superclass constructor, Java implicitly calls the superclass’s default or no-argument constructor. A class’s default constructor calls the superclass’s
Education, Inc. All Rights Reserved. 15 toString is one of the methods that every class inherits directly or indirectly from class Object. Returns a String representing an object. Called implicitly whenever an object must be converted to a String representation. Class Object’s toString method returns a String that includes the name of the object’s class. This is primarily a placeholder that can be overridden by a subclass to specify an appropriate String representation.
Education, Inc. All Rights Reserved. 16 To override a superclass method, a subclass must declare a method with the same signature as the superclass method @Override annotation
Indicates that a method should override a superclass method
with the same signature. If it does not, a compilation error occurs.
Education, Inc. All Rights Reserved. 17 Class BasePlusCommissionEmployee (Fig. 9.6) contains a first name, last name, social security number, gross sales amount, commission rate and base salary. All but the base salary are in common with class CommissionEmployee. Class BasePlusCommissionEmployee’s public services include a constructor, and methods earnings, toString and get and set for each instance variable Most of these are in common with class CommissionEmployee.
Education, Inc. All Rights Reserved. 18 Class BasePlusCommissionEmployee does not specify “extends Object” Implicitly extends Object. BasePlusCommissionEmployee’s constructor invokes class Object’s default constructor implicitly.
Education, Inc. All Rights Reserved. 19 Much of BasePlusCommissionEmployee’s code is similar, or identical, to that of CommissionEmployee. private instance variables firstName and lastName and methods setFirstName, getFirstName, setLastName and getLastName are identical. Both classes also contain corresponding get and set methods. The constructors are almost identical BasePlusCommissionEmployee’s constructor also sets the base-Salary. The toString methods are nearly identical BasePlusCommissionEmployee’s toString also outputs instance variable baseSalary
Education, Inc. All Rights Reserved. 20 We literally copied CommissionEmployee’s code, pasted it into BasePlusCommissionEmployee, then modified the new class to include a base salary and methods that manipulate the base salary. This “copy-and-paste” approach is often error prone and time consuming. It spreads copies of the same code throughout a system, creating a code-maintenance nightmare.
Education, Inc. All Rights Reserved. 21 Class BasePlusCommissionEmployee class extends class CommissionEmployee A BasePlusCommissionEmployee object is a CommissionEmployee Inheritance passes on class CommissionEmployee’s capabilities. Class BasePlusCommissionEmployee also has instance variable baseSalary. Subclass BasePlusCommissionEmployee inherits CommissionEmployee’s instance variables and methods Only the superclass’s public and protected members are directly accessible in the subclass.
Education, Inc. All Rights Reserved. 26 Class BasePlusCommissionEmployee (Fig. 9.11) has several changes that distinguish it from Fig. 9.9. Methods earnings and toString each invoke
their superclass versions and do not access instance