Question

In: Computer Science

This violates Liskov Substitution Principle (LSP), can someone explain why and refactor it so it does...

This violates Liskov Substitution Principle (LSP), can someone explain why and refactor it so it does not?

  
public abstract class Bird
    {
        public abstract void LayEgg();
        public abstract void FlyAway();
    }
    public class Penguin : Bird
    {
        public override void LayEgg()
        {
            //Make husband penguin do all the incubation work.
        }
        public override void FlyAway()
        {
            //Penguins can't fly.
            throw new NotImplementedException();
        }

        public void SwimAway()
        {
            //Swimming is way more fun.
        }
    }

Solutions

Expert Solution

Liskov substitution principle defines that objects of a superclass(Base class) shall be replaceable with objects of its subclasses(derived class) without breaking the application. This requires the objects of your subclasses to behave in the same way as the objects of your superclass.

So thats the basic idea of Liskov substitution principle. In the sample code that you gave we have a super class Bird and a sub class Penguin.

So class definition here is Penguin is a Bird, which can also be written as all Penguins are Birds but not all Birds are Penguins. So A Penguin must have all the basic(abstract) definitions of a Bird. we have two abstract methods in Bird LayEgg() and FlyAway(). Penguin implements LayEgg() but not FlyAway(), since penguins cannot fly. This violates the Liskov Substitution principle.

This can be refactored by further introducing two subclasses under Bird namely FlyingBird and FlightlessBird. And inheriting Penguin from the class FlightlessBird.

By doing this we simplify the definition of bird to "All Birds Lays Eggs" which is much simpler than All Birds lay Eggs and Fly". We delegate the other definition to next level of subclasses which introduce their own definitions, without changing the original definition of the Bird.

Refactored code:

public abstract class Bird { 
    public abstract void LayEgg(); 
} 
public abstract class FlyingBird : Bird {
    public abstract void FlyAway();
}
public abstract class FlightlessBird : Bird
{
}


public class Penguin:FlightlessBird
{
    public override void LayEgg()
    {
        //Make husband penguin do all the incubation work. 
    }
    public void SwimAway()
    {
        //Swimming is way more fun. 
    }
}

Inheritance diagram:


Related Solutions

This violates the ISP principle in SOLID, can someone explain why and refactor? Thank you! public...
This violates the ISP principle in SOLID, can someone explain why and refactor? Thank you! public interface IGesture { void OnClick(); void OnSwipe(); void OnDoubleClick(); } public class LaunchButton : IGesture { public void OnClick() { //Launch nuclear missiles } public void OnDoubleClick() { throw new NotImplementedException(); } public void OnSwipe() { throw new NotImplementedException(); } }
Can someone explain to me why does increasing the electrolyte concentration can increase the electroplating rate/or...
Can someone explain to me why does increasing the electrolyte concentration can increase the electroplating rate/or metal deposit...? Take copper sulphate as an example. Better explanation with pics...And just don't go too far beyond the Faraday's law and please try to explain it in a more 'Chemistry' way. Thanks!
Can someone explain in terms of physics why or why not a strong magnet can damage...
Can someone explain in terms of physics why or why not a strong magnet can damage a laptop screen.
Why does an increase in the interest rate generate a substitution effect? Why does it produce...
Why does an increase in the interest rate generate a substitution effect? Why does it produce an income effect? Does the substitution effect depend on whether one is a saver or a borrower? What about the income effect?
Can someone explain why the answer to this question is false? If the same quantities are...
Can someone explain why the answer to this question is false? If the same quantities are supplied by firms, but at higher prices, supply has increased a.) true b.) false
Can you think of a firm that violates the concept of mean reversion? Why do you...
Can you think of a firm that violates the concept of mean reversion? Why do you think this occurs?
Can you explain why Eindtein never accepted Heisenberg’s uncertainty principle ???
Can you explain why Eindtein never accepted Heisenberg’s uncertainty principle ???
can someone change this code so that timestandard can be calculated at the end of the...
can someone change this code so that timestandard can be calculated at the end of the code using the inputs n,RF,PFD, and the measured cycles, instead of being called from the beginning of the code using namespace std; float timestandard(float time, float rf, float pfd) { return((time / 100) * rf * (1 + pfd)); /* calculating the time standard using the given formula*/ } int main() { int n, rf, pfd, x; /* inputs are taken*/ cout << "****...
Discuss the matching principle. Why is it so fundamental to accrual accounting?
Discuss the matching principle. Why is it so fundamental to accrual accounting?
Explain why deflation can be so troubling to borrowers and lenders.
Explain why deflation can be so troubling to borrowers and lenders.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT