...
Code Block | ||
---|---|---|
| ||
private class Account { // Maintains all banking related data such as account balance private double balance = 100; boolean withdraw(double amount) { if ((balance - amount) >= 0) { balance -= amount; System.out.println("Withdrawal successful. The balance is : " + balance); return true; } return false; } } public class BankAccount extends Account { // Subclass handles authentication @Override boolean withdraw(double amount) { if (!securityCheck()) { throw new IllegalAccessException(); } return super.withdraw(amount); } private final boolean securityCheck() { // check that account management may proceed } } public class Client { public static void main(String[] args) { Account account = new BankAccount(); boolean result = account.withdraw(200.0); // Enforce security manager check System.out.println("WithdrawWithdrawal successful? " + result); } } |
...
Code Block | ||
---|---|---|
| ||
private class Account { // Maintains all banking related data such as account balance boolean overdraft() { balance += 300; // Add 300 in case there is an overdraft System.out.println("Added back-up amount. The balance is :" + balance); return true; } // other Account methods } public class BankAccount extends Account { // Subclass handles authentication //NOTE: unchanged from previous version //NOTE: lacks override of overdraft method } public class Client { public static void main(String[] args) { Account account = new BankAccount(); boolean result = account.withdraw(200.0); // Enforce security manager check if (!result) { result = account.overdraft(); } System.out.println("WithdrawlWithdrawal successful? " + result); } } |
...
Code Block |
---|
public class MaliciousClient { public static void main(String[] args) { Account account = new BankAccount(); boolean result = account.overdraft(200.0); // No security check performed System.out.println("WithdrawlWithdrawal successful? " + result); } } |
...
Code Block | ||
---|---|---|
| ||
class BankAccount extends Account {
// ...
@Override void overdraft() { // override
throw new IllegalAccessException();
}
}
|
Alternately, when the intended design permits the new method in the parent class to be invoked directly from a subclass without overriding, install a security manager check directly in the new method.
...
Wiki Markup |
---|
The {{Provider}} class inherits the {{put()}} and {{remove()}} methods from {{Hashtable}} and adds security manager checks to each. These checks ensure that malicious code cannot add or remove the mappings. When {{entrySet()}} was introduced, it became possible for untrusted code to remove the mappings from the {{Hashtable}} because {{Provider}} did not override this method to provide the necessary security manager check \[[SCG 2007|AA. Bibliography#SCG 07]\]. This problem is commonly knowknown as a "fragile class hierarchy" in other object-oriented languages, such as C+\+. |
Noncompliant Code Example (Calendar
)
...
Code Block | ||
---|---|---|
| ||
class CalendarSubclass extends Calendar { @Override public boolean after(Object when) { // correctly calls Calendar.compareTo() if (when instanceof Calendar && super.compareTo((Calendar) when) == 0) { return true; } return super.after(when); } @Override public int compareTo(Calendar anotherCalendar) { return compareDays(this.getFirstDayOfWeek(), anotherCalendar.getFirstDayOfWeek()); } private int compareDays(int currentFirstDayOfWeek, int anotherFirstDayOfWeek) { return (currentFirstDayOfWeek > anotherFirstDayOfWeek) ? 1 : (currentFirstDayOfWeek == anotherFirstDayOfWeek) ? 0 : -1; } public static void main(String[] args) { CalendarSubclass cs1 = new CalendarSubclass(); cs1.setTime(new Date()); cs1.set( Calendar.DAY_OF_WEEK, Calendar.SUNDAY); // Date of last Sunday (before now) CalendarSubclass cs2 = new CalendarSubclass(); // Wed Dec 31 19:00:00 EST 1969 System.out.println(cs1.after(cs2)); // expected to print true } // Implementation of other Calendar abstract methods } |
Such errors generally occur because the developer has depended on assumptions about the implementation-specific details of the superclass. Even when these assumptions are initially correct, implementation details of the superclass may change without warning.
Wiki Markup |
---|
The {{java.util.Calendar}} class provides a {{compareTo()}} method and an {{after()}} method. The {{after()}} method is documented in (\[[API 2006|AA. Bibliography#API 06]\]) as follows: |
The
after()
method returns whether thisCalendar
represents a time after the time represented by the specifiedObject
. This method is equivalent to
compareTo(when) > 0
if and only ifwhen
is aCalendar
instance. Otherwise, the method returnsfalse
.
...
In this case, the two objects are initially compared using the overriding CalendarSubclass.after()
method. This invokes the superclass's Calendar.after()
method to perform the remainder of the comparison. But the Calendar.after()
method internally calls the compareTo()
method, which is delegated to CalendarSubclass.compareTo()
. Consequently, CalendarSubclass.after()
actually calls CalendarSubclass.compareTo()
and returns false
.
...
Code Block | ||
---|---|---|
| ||
// The CalendarImplementation object is a concrete implementation of the abstract Calendar class // Class ForwardingCalendar public class ForwardingCalendar { private final CalendarImplementation c; public ForwardingCalendar(CalendarImplementation c) { this.c = c; } CalendarImplementation getCalendarImplementation() { return c; } public boolean after(Object when) { return c.after(when); } public int compareTo(Calendar anotherCalendar) { // CalendarImplementation.compareTo() will be called return c.compareTo(anotherCalendar); } } class CompositeCalendar extends ForwardingCalendar { public CompositeCalendar(CalendarImplementation ci) { super(ci); } @Override public boolean after(Object when) { // This will call the overridden version, i.e. CompositeClass.compareTo(); if (when instanceof Calendar && super.compareTo((Calendar)when) == 0) { // Return true if it is the first day of week return true; } return super.after(when); // Does not compare with first day of week any anymorelonger; // Uses default comparison with epoch } @Override public int compareTo(Calendar anotherCalendar) { return compareDays(super.getCalendarImplementation().getFirstDayOfWeek(), anotherCalendar.getFirstDayOfWeek()); } private int compareDays(int currentFirstDayOfWeek, int anotherFirstDayOfWeek) { return (currentFirstDayOfWeek > anotherFirstDayOfWeek) ? 1 : (currentFirstDayOfWeek == anotherFirstDayOfWeek) ? 0 : -1; } public static void main(String[] args) { CalendarImplementation ci1 = new CalendarImplementation(); ci1.setTime(new Date()); ci1.set( Calendar.DAY_OF_WEEK, Calendar.SUNDAY); // Date of last Sunday (before now) CalendarImplementation ci2 = new CalendarImplementation(); CompositeCalendar c = new CompositeCalendar(ci1); System.out.println(c.after(ci2)); // expected to print true } } |
Note that each method of the class ForwardingCalendar
redirects to methods of the contained CalendarImplementation
class, from which it receives return values; this is the forwarding mechanism. The ForwardingCalendar
class is largely independent of the implementation of the class CalendarImplementation
. Consequently, future changes to CalendarImplementation
are unlikely to break ForwardingCalendar
and are also unlikely to break CompositeCalendar
. Invocations of the overriding after()
method of CompositeCalendar
perform the necessary comparison by using the CalendarImplementation.compareTo()
method as required. Using super.after(when)
forwards to ForwardingCalendar
, which invokes the CalendarImplementation.after()
method as required. As a result, ava.util.Calendar.after()
invokes the CalendarImplementation.compareTo()
method as required, resulting in the program correctly printing true
.
...