CSC/ECE 517 Fall 2009/wiki3 20 i7: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 1: Line 1:
= Liskov substitution principle =
= Liskov substitution principle =
The Liskov substitution principle was introduced by Barbara Liskov in 1987 and is concerned with subtype contractual correctness.  Based on a paper from 1994 the principle states, "Let q(x) be a property provable about objects x of type T. Then q(y) should be true for objects y of type S where S is a subtype of T.".  This means that for every parent type T, subtype S should be able to be substituted in for it and the behavior of the program should remain exactly the same.  The following example in Java illustrates the the principle.
The Liskov substitution principle was introduced by Barbara Liskov in 1987 and is concerned with subtype contractual correctness.  Based on a paper from 1994 the principle states, "Let q(x) be a property provable about objects x of type T. Then q(y) should be true for objects y of type S where S is a subtype of T.".  This means that for every parent type T, subtype S should be able to be substituted in for it and the behavior of the program should remain exactly the same.  The following example in Java illustrates the the principle.
<pre>
public class MyClass {
    private final List<String> list;
    public MyClass(List<String> list) {
        this.list = list;
    }
    ....
}
public class TestRunner {
    public static void main ( String[] args ) {
        // If the List type follows Liskov substitution...
        List<String> arrayList = new ArrayList<String>();
        List<String> linkedList = new LinkedList<String>();
        // ...I should be able to use any subtype of list to create a new MyClass!
        MyClass this = new MyClass(arrayList);
        MyClass works = new MyClass(linkedList);
    }
}
</pre>


==Preconditions==
==Preconditions==

Revision as of 09:04, 18 November 2009

Liskov substitution principle

The Liskov substitution principle was introduced by Barbara Liskov in 1987 and is concerned with subtype contractual correctness. Based on a paper from 1994 the principle states, "Let q(x) be a property provable about objects x of type T. Then q(y) should be true for objects y of type S where S is a subtype of T.". This means that for every parent type T, subtype S should be able to be substituted in for it and the behavior of the program should remain exactly the same. The following example in Java illustrates the the principle.


public class MyClass {
    private final List<String> list;
    public MyClass(List<String> list) {
        this.list = list;
    }
    ....
}

public class TestRunner {
    public static void main ( String[] args ) {
        // If the List type follows Liskov substitution...
        List<String> arrayList = new ArrayList<String>();
        List<String> linkedList = new LinkedList<String>();

        // ...I should be able to use any subtype of list to create a new MyClass!
        MyClass this = new MyClass(arrayList);
        MyClass works = new MyClass(linkedList);
    }
}

Preconditions

Preconditions cannot be strengthened. <Explain in english> <Example>

Postconditions

Postconditions cannot be weakened. <Explain in english>

Invariants

Invariants must be preserved. <Explain in english>

References