Java Reference
In-Depth Information
Some aspects of LSP compliance require human-level intelligence, or at least more
intelligence than today's compilers possess. For example, does code that sets a collection of
fireworks to be nonexploding violate LSP?
public void defuse(FireworkList list)
{
for (int i = 0; i < list.size(); i++)
{
Firework f = list.get(i);
f.setExplodes(false);
}
}
The Firework class provides the setExplodes() method so that you can specify whether
certain types of fireworks explode. In particular, rockets may or may not explode. The
setExplodes() method is useful for rockets, but some subclasses of Firework cannot
entirely support this operation. It is a modeling error, for example, to indicate that a
Firework instance explodes if that instance is in fact an instance of Sparkler . The model
violates LSP because subclasses do not support a superclass method. However, violations of
LSP are not necessarily bad. In this example, a modeling error occurs only when
setExplodes() is called with an argument that doesn't make sense for a particular type of
firework.
Violations of LSP can occur outside of domain-specific code and can occur in the Java class
libraries. Consider a program that converts an array to a list, prints it, and adds an element to
the list:
package com.oozinoz.applications;
import java.util.*;
public class ShowAsList
{
public static void main(String[] args)
{
String[] names =
new String[] {
"Mixer", "Star Press", "Shell Assembler" };
System.out.println(names);
List L = Arrays.asList(names);
System.out.println(L);
L.add("Fuser");
}
}
This program prints two lines of text and then crashes:
[Ljava.lang.String;@7259da
[Mixer, Star Press, Shell Assembler]
Exception in thread
"main"java.lang.UnsupportedOperationException
at java.util.AbstractList.add(Unknown Source)
...
Search WWH ::




Custom Search