Navneet Garg
Navneet Garg

Reputation: 1374

Liskov Substitution Principle Violation Rules

The purpose of inheritance is to inherit and extend.

So my question is that if child class have more method that will it break the LSP ? So in my example I have 2 classes Rectangle and Square. Square is child of Rectangle. Now both have 2 different methods HelloRectangle and HelloSquare. So will it breack LSP or not ?

public class Rectangle
{
    //public int Width { get; set; }
    //public int Height { get; set; }

    public virtual int Width { get; set; }
    public virtual int Height { get; set; }

    public Rectangle()
    {

    }

    public Rectangle(int width, int height)
    {
        Width = width;
        Height = height;
    }

    public override string ToString()
    {
        return $"{nameof(Width)}: {Width}, {nameof(Height)}: {Height}";
    }
    public string HelloRectangle()
    {
        return "Hello Rectangle";
    }
}

public class Square : Rectangle
{
    

    public override int Width // nasty side effects
    {
        set { base.Width = base.Height = value; }
    }

    public override int Height
    {
        set { base.Width = base.Height = value; }
    }
    public string HelloSquare()
    {
        return "Hello Square";
    }
}

Upvotes: 2

Views: 190

Answers (2)

jaco0646
jaco0646

Reputation: 17104

The substitutability defined by the LSP applies in only one direction: a child may replace its parent. It is never the case that a parent is expected to replace its child, irrespective of the LSP.

In statically-typed languages, substitution is only possible in one direction. For example, in Java we can show that substitution only compiles when Child replaces Parent.

class Parent {
    void method() {
        System.out.println("Hello Parent");
    }
}

class Child extends Parent {
    void anotherMethod() {
        System.out.println("Hello Child");
    }
}

class Main {
    public static void main(String... args) {
        Parent parent = new Parent();
        Child child = new Child();
        useParent(child);  // compiles: Child is syntactically substitutable for Parent
        useChild(parent);  // error: Parent is not syntactically substitutable for Child.
    }

    static void useParent(Parent parent) {
        parent.method();
    }

    static void useChild(Child child) {
        child.anotherMethod();
    }
}

It's not a problem that Child has anotherMethod(), because Child cannot be replaced by Parent.

Upvotes: 0

Pradeep Kumar
Pradeep Kumar

Reputation: 1526

child class have more method that will it break the LSP ? :- Not it will not break LSP unless you change the behavior of common method. When you substitute child class then you can only access method which drive from base regardless of more/other methods in child class for example

Public class A
{
  public int sum ()
  {
  }
}

public class B
{
  public int sum ()
  {
// same behaviour as per method in A
  }
  
  public int divide()
  {
  }
}

// So calling of method 

B b = new B();
b.Sum();

//So If you substitute with drive class then
B b = new A();
// Here in the below line there is no impact of  other methods of child class Thus child class have more method will not break the LSP 
b.Sum(); 
  

Hence both have 2 different methods HelloRectangle and HelloSquare. So it will not break LSP.

But on other hand setwidth and setheight break LSP because in case of square, behavior of setwidth and setheight has been changed

I hope you got the answer and not confused with setwidth and setheight method in your case.

Upvotes: 0

Related Questions