Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does one child implementing an interface, but another not, violate the Liskov Substitution Principle?

I've been thinking recently about the Liskov Substitution Principle and how it relates to my current task. I have a main form which contains a menu; into this main form I will dock a specific form as a MDI child. This specific form may or may not contain a menu. If it does, I want to merge it with the main menu. So the situation is like this:

public class MainForm
{
    public void Dock(SpecificBaseForm childForm)
    {
        this.Dock(childForm);
        if (childForm is IHaveMenu)
        {
            this.Menu.Merge(childForm as IHaveMenu).Menu;
        }
    }
 }

public interface IHaveMenu
{
   Menu Menu {get;}
}

public abstract class SpecificBaseForm{}

public class SpecificFormFoo : SpecificBaseForm {}

public class SpecificFormBar: SpecificBaseForm,IHaveMenu
{
    public Menu Menu{get {return this.Menu;}}
}

Now the two children are not "exactly" interchangeble: one has a menu; the other doesn't. Is this a misuse of the is/as contruct in C#? If this is not correct, what would be the clean solution?

like image 979
Biggles Avatar asked Jan 13 '14 13:01

Biggles


People also ask

How do you not violate the Liskov Substitution Principle?

There are several possible ways: Returning an object that's incompatible with the object returned by the superclass method. Throwing a new exception that's not thrown by the superclass method. Changing the semantics or introducing side effects that are not part of the superclass's contract.

Does Liskov Substitution Principle apply to interfaces?

In other words, if an interface method implemented by a class is semantically different from what user expects it to be, would this be considered as a violation of LSP? Yes. Exactly same reasons and results as violating LSP if it's an interface, an abstract class, a full class, doesn't matter.

What is the most accurate example of liskov of substitution principle?

A good example here is that of a bird and a penguin; I will call this dove-penguin problem. The below is a Java code snippet showing an example that violates the LSP principle. Here, the Dove can fly because it is a Bird. In this inheritance, much as technically a penguin is a bird, penguins do not fly.

What is LSP Liskov Substitution Principle and what are some examples of its use?

Simply put, the Liskov Substitution Principle (LSP) states that objects of a superclass should be replaceable with objects of its subclasses without breaking the application. In other words, what we want is to have the objects of our subclasses behaving the same way as the objects of our superclass.


2 Answers

No, it's not a violation of the Liskov substitution principle. Whether the specific sub classes implement different interfaces don't matter as long as they can be used in place of the base class.

Also keep in mind that the LSP is about the behaviour of the class, not about the signatures of methods or whether they implement an additional interface or not.

like image 78
Dirk Avatar answered Sep 23 '22 20:09

Dirk


The Liskov substitution principle states that the base classes must be able to be replaced by their more derived subtypes. The subclasses don't need to be able to replace each other, as long as each individually can replace the base class without breaking the program or otherwise causing it to not function as desired. So the instance you described does not violate the LSP.

like image 38
starsplusplus Avatar answered Sep 21 '22 20:09

starsplusplus