ships19
ships19

Reputation: 43

Is the Liskov Principle violated if overridden methods return a different value?

Below is my base class which has ReturnAddress method which returns 'Address one' and in child class Im overriding same method which return 'Address two'.

public class Base     
{         
     public virtual string ReturnAddress()        
     {             
         return "Address one";         
     }    
 }    


public class Derived : Base     
{                  
    public override string ReturnAddress()         
     {             
        return "Address Two";         
     }
}

//Object declaration for base and derived

Base base = new Base();

Derived der = new Derived();

var result = der.ReturnAddress(); // will return "Address Two"
but if we replace der object with base object 

var result = base.ReturnAddress(); //Will return "Address One"

So child object is not replacable by parent object.

I want to know that is above example breaking Liskov Principle ?

Upvotes: 3

Views: 447

Answers (3)

Theodoros Chatzigiannakis
Theodoros Chatzigiannakis

Reputation: 29213

Reading from the description:

  • The types are substitutable (they are both reference types).
  • Covariance is not violated in return types (they happen to remain the same).
  • Contravariance is not violated in inputs (since there are no inputs).
  • No new exceptions are candidates to be thrown (both implementations call the same method).
  • No preconditions are strengthened (there are no obvious preconditions in your particular example).
  • No postconditions are weakened (as above).
  • No invariants are modified (since both classes happen to be stateless).
  • The history rule isn't violated (since the derived class doesn't introduce mutable state that could be observable from the base class).

According to the above, you're not violating LSP.

But if the rest of your program has some requirements that we can't infer from these two classes (for example, perhaps only one specific string is acceptable as the return value of these objects), then these classes aren't useful to you, regardless of whether they respect or violate LSP.

Upvotes: 3

Servy
Servy

Reputation: 203804

The LSP doesn't state that a child object will always do exactly the same thing as a parent object, when you treat it as the parent object. There would be no purpose to inheriting from an object if that was the case.

Rather, the point is that you can treat a Base object as if it's a Base object or any object derived from it. The code should work regardless of whether the actual object instance is of type Base or Derived. Now if your code will fail if Address Two is returned, and it expects Address One to be returned, then you'll have broken LSP. If your code will work regardless of which string is returned, so long a string is returned, then you're fulfilling the LSP.

The LSP states that you should be able to replace objects of one type with objects of the other, "without altering any of the desirable properties of that program" (taken from the wikipedia page). Those desirable properties could be, "has a property that returns a string" or it could be "has a property that returns 'Address One'". What you consider "desirable" about that object determines whether or not the LSP is violated.

Upvotes: 10

BanksySan
BanksySan

Reputation: 28500

No, it is not.

That is, unless you are changing the meaning of address.

For example, if the base was a postal address, but the derived was an IP address, the Gettysburg Address or a misspelling of 'a dress'. Whilst these have the same word, and so the compiler won't be clever enough to stop you, they are very different words.

In other words, you aren't playing linguistic tricks on the word address.

Upvotes: 4

Related Questions