#How does LSP works in OOP
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
which part of it is unclear to u
u recently learned about "is-a" relationship. so i guess that's unclear to u as well?
Animal a = new Dog(); is this line unclear to u?
or is it clear but u didnt get the motivation for it yet?
@dusky ginkgo
Your question has been closed due to inactivity.
If it was not resolved yet, feel free to just post a message below
to reopen it, or create a new thread.
Note that usually the reason for nobody calling back is that your
question may have been not well asked and hence no one felt confident
enough answering.
When you reopen the thread, try to use your time to improve the quality
of the question by elaborating, providing details, context, all relevant code
snippets, any errors you are getting, concrete examples and perhaps also some
screenshots. Share your attempt, explain the expected results and compare
them to the current results.
Also try to make the information easily accessible by sharing code
or assignment descriptions directly on Discord, not behind a link or
PDF-file; provide some guidance for long code snippets and ensure
the code is well formatted and has syntax highlighting. Kindly read through
https://stackoverflow.com/help/how-to-ask for more.
With enough info, someone knows the answer for sure 👍
sorry, i didnt saw the nessage, yes, ig LSP says that a class can be replaced by its subclases, but i dont understand why would i do:
"Animal a = new Dog();"
instead of directly saying:
"Dog dog = new Dog();"
like i dont understand for what it is useful for
Think of it not at this point in the code (that was not the original intent of LSP) but at the edges of the class.
Compare my class offering one of these two methods
public void add(ArrayList<Animal> animals) { ... }
public void add(Collection<? extends Animal> animals) { ... }
One allows anyone to substitute other Collection types, and with generic specialisations of Animal.
Taken to the extreme, anywhere where you have a use-site of a type, if a more general type would be acceptable to the pattern of use, using that type makes it less disruptive and limiting to substitute another type.
Similarly...
List<Foo> list = new ArrayList<>();
Means that all users of list are assuming nothing about the actual list type, and I could change my mind later and use LinkedList without changing any other code.
Note that the original papers and discussions where about the interfaces between units of code. Often people oversell the LSP intent to mean a strict law that applies to all internal (private) content as well, rather than being more specific about a type-choice - that specificity can express intent (choosing a concrete type for it behavioural, not linguistic properties), and can (somewhat rarely) slightly improve the chances of monomorphic optimisations.
All of these rules are 'good things to consider' not absolute laws - because there's almost always a case in which doing otherwise is the better choice (once all aspects of the project are considered). Software Development does tend to become a little dogmatic though, and people are rewarded for books and videos ranting on the topic which doesn't help.
its confusing to u now bc ur not yet at a point where u have to design libraries/apis
especially in bigger code bases these things become relevant
or when u write code for others to use
when ur methods demand a type, it should be the most vague type possible that still fulfills all ur requirements
so for example Iterable instead of ArrayList if all u do with it is iterating it
So that means that the LSP is something that helps you make code more readable and useful
It makes it easier to maintain code.
Oh okey
ohh
Well i then ig thats something i have to see later right?
it makes code more modular and flexible
a method whose parameters use LSP can be used by more classes and make more people happy
a shelter for animals should take any animal
not just dogs
or cats
but both, dogs, cats, also birds, any animal can be taken
addAnimal(Animal a)
and not addAnimal(Dog d)
thats LSP in practice
a method that feeds something should can maybe an interface CanBeFed
instead of Dogs only
bc then it can also feed humans, fish, ...
everything that implements the interface
write the most vague type that still fulfills ur requirements
then it can be used by the most classes
And in Java, interface-types are less constraining than class-types, so we favour using interface-types when possible.
ty