problem about lsp-Collection of common programming errors
FredOverflow
java exception inheritance exception-handling lsp
I find Java’s exception hierarchy confusing. Throwable is divided into Error and Exception, and RuntimeException inherits from Exception.Error is an unchecked exception. Why doesn’t Error inherit from RuntimeException then? Exception is a checked exception. RuntimeException is an unchecked exception, yet it inherits from Exception. Doesn’t this violate the Liskov-Substitution-Principle?Wouldn’t it make more sense if Throwable were divided into Exception (checked) and RuntimeException (unchecked)
Chris Paynter
lsp ocp
I am solidifying my understanding of the relationship between Liskov Substitutional Principal and Open Close Principal. If anybody could confirm my deductions and answer my questions below that would be great.I have the following classes. As you can see, B is derived from A and it is overriding the DisplayMessage function in order to alter the behavior.public class A {private readonly string _message;public A(string message){_message = message;}public virtual void DisplayMessage(){Console.WriteL
paxdiablo
ruby oop lsp
I’ve read some of article about the practice that making Square an inheritance class of Rectangle class is a bad practice, saying it violate the LSP (Liskov substitution principle). I still don’t get it, I made a example code in Ruby:class Rectangleattr_accessor :width, :heightdef initialize(width, height)@width = width@height = heightend endclass Square < Rectangledef initialize(length)super(length, length)enddef width=(number)super(number)@height = numberenddef height=(number)super(number)
eytanfb
java inheritance lsp
I have a model abstract class which declares a List of items. The abstract has two abstract class. One in which you can add new items to the list and one that doesn’t use the list at all but adheres otherwise to the other behavior of the model abstract class.I declared two methods to add and remove items from the list. Obviously, whenever I want to use those methods I need to cast my model abstract with its subclass. Can I violate LSP (Liskov substitution principle) in this case? Or is there a w
????
oop solid-principles lsp
I’m very new to the SOLID design principles. One thing I had problem with understanding is the “Square-rectangle” example of a Liskov Substition Principle violation. Why should the Height/Width setter of a Square override the ones of a Rectangle? Isn’t this exactly what’s causing the problem when there’s Polymorphism?Doesn’t removing this solve the problem? class Rectangle {public /*virtual*/ double Height { get; set; }public /*virtual*/ double Width { get; set; }public double Area() { return He
Charles
c# polymorphism solid-principles lsp
The LSP says “The derived types must not change the behavior of the base types”, in other words “Derived types must be completely replaceable for their base types.” This means that if we define virtual methods in our base classes, we have violated this principle.Also if we hide a method in the drive method by using new keyword then again we have violated this principle.In other words, if we use polymorphism we have violated LSP!In many applications I’ve used Virtual methods in the base classes a
Aidan
c# inheritance lsp
I am using an API that violates the Liskov substitution principle : it throws its own Exception type that extends Exception, but puts the exception message from the base class in a new ErrorCode field and puts its own (useless) message in the Message field. Therefore to display the correct message I need to cast the Exception to the DerivedException type and use the ErrorCode field. If I treat it as an Exception object I get the wrong message.Now this irks me on a stylistic level, but it is easy
????
oop object-oriented-analysis lsp
There was a book that talks about have a PhoneNumber class, and then we would define an Address class that inherits from PhoneNumber, and I said at one time, that we can’t do that, because an address is not a phone number, and to inherit, it must be a “is a” relationship. Such as: a dog is an animal, and we we can make Dog inherit from Animal.But since we have to follow LSP — Liskov Substitution Principle, then the “is a” rule actually is not the determining factor here, because a square “is a
Web site is in building
I discovery a place to host code、demo、 blog and websites.
Site access is fast but not money