Inheritance
Inheritance
} Inheritance (继承)
} Overriding (重写)
} 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.
} 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 (there will be an example later)
Note that this is for controlling access to class members. At the top level, a class
can only be declared as public or package-private (no explicit modifier)
} Differences
§ BasePlusCommissionEmployee class needs one additional
field to store the employee’s base salary
§ The way of calculating the earnings are different
Data fields
(attributes)
A five-argument constructor
Identical
Identical
extends Object
@Override
public String toString() {
return String.format("%s: %s %s\n%s: %s\n%s: %.2f\n%s: %.2f",
"commission employee", firstName, lastName,
"social security number", socialSecurityNumber,
"gross sales", grossSales,
"commission rate", commissionRate);
}
} The access level of an overriding method can be higher, but not lower than
that of the overridden method (package-private < protected < public)
} @Override annotation
§ Optional, but helps the compiler to ensure that the method has the same
signature as the one in the superclass
@Override
public String toString() { … }
} If this is not explicitly done, the compiler automatically inserts a call to the
no-argument constructor of the superclass. If the super class does not have
a no-argument constructor, you will get a compile-time error.
@Override
public String toString() {
return String.format("%s: %s %s\n%s: %s\n%s: %.2f\n%s: %.2f\n%s: %.2f",
"base-salaried", firstName, lastName,
"social security number", socialSecurityNumber,
"gross sales", grossSales,
"commission rate", commissionRate,
"base salary", baseSalary);
}
Compilation error
} If the subclass is in the same package as its parent, it also inherits the
parent’s package-private members (those without access level modifiers)
§ These members are directly accessible in the subclass
} A subclass does not inherit the private members. Private fields need to be
accessed using the methods (public, protected, or package-private ones)
inherited from superclass.
public Y Y Y Y
protected Y Y Y N
no modifier Y Y N N
private Y N N N
Note that this is for controlling access to class members. At the top level, a class
can only be declared as public or package-private (no explicit modifier)
vs.
public double earnings() {
return baseSalary + ( getCommissionRate() * getGrossSales() );
}
vs.
public double earnings() {
return baseSalary + ( commissionRate * grossSales );
}
} By doing this, you can reuse the fields and methods of the existing class
without having to write (and debug!) them yourself.