Tommy Sin

Tommy Sin

  • NA
  • 19
  • 104.2k

Regarding Liskov substitution principle

May 31 2011 4:01 PM
I read an article saying

Liskov substitution principle (LSP) is a principle in object-oriented programming. It states that, in a computer program if S is a subtype of T, then objects of type T may be replaced with objects of type S (i.e., objects of type S may be substitutes for objects of type T), without altering any of the desirable properties of that program (correctness, task performed, etc.).




I believe I understand most of it but I have one concern; what happens if overring occurs in the subType? Is it not going to change the correctness and task performed by the object of the subtype S that replaces the object of the type T?

For ex,

Class S
{
        public void virtual GetNumber()
        {
                return 4;
        }
}

Class T : S
{
       public void override GetNumber()
       {
                return 10;
       }
}

In this case, if an object of type T replaces the object of Type S, I will change the correctness and task performed.
 
Can anyone help?
Thanks in advance,

Answers (1)