1.1 Liskov Substitution Principle (LSP) with Solution in Java - SOLID Principles of Low Level Design

Поділитися
Вставка
  • Опубліковано 3 тра 2023
  • this is one of the important SOLID principle. And covered its both Problem and Solution
    ➡️ Become a Member: / @conceptandcoding

КОМЕНТАРІ • 117

  • @ankitgoyal8556
    @ankitgoyal8556 3 місяці тому +5

    UA-cam and people like you have made life easier, thankuu.

  • @nitigya
    @nitigya 10 місяців тому +5

    This brings the point when to ditch Inheritance over Composition. Like how Abstraction/Inheritance brings coupling along with itself. Though, both Inheritance and Composition will have it's own pros/cons. Amazing playlist!

  • @rajugoud6465
    @rajugoud6465 17 днів тому +1

    This is the perfect Explanation of this principle, i watched this principle in paid courses, but this is the perfect explanation

  • @Ricky-jr7io
    @Ricky-jr7io Рік тому +6

    An excellent explanation of Liskov's Substitution principle. I work primarily with python and javascript, and your explanation really illustrates well why typescript and type hinting in python are necessary to avoid errors.
    Thank you very much.

  • @ashishgoswami6303
    @ashishgoswami6303 8 місяців тому +2

    Awesome work ! I have been working for a while but you cleared concepts with examples!

  • @CanadaKeBhai
    @CanadaKeBhai Рік тому +7

    Best example of Liskov's substitution principle

  • @anuraggupta6277
    @anuraggupta6277 11 місяців тому +13

    One suggestion - hasEngine could have values - true and false. So instead of returning null, we could have returned false, and it would not break.
    For this example, we could have taken something like engineType instead of engine. In that case, cycle will have to return null.
    PS - I am new to these topics. Please correct me if i am wrong 🙂

    • @ConceptandCoding
      @ConceptandCoding  11 місяців тому +2

      :) yes, i think you got the point.
      Enginetype can be better functionality.
      but hasEngine is returning Boolean class type, so null is one of the valid return type here.

    • @janpost8598
      @janpost8598 9 місяців тому

      I was thinking the same thing. But then a car who should have an engine could have no (hasEngine returns false) engine. So it perfectly illustrates why a bicycle returns a null and shouldn't have the hasEngine method in the first place.

    • @codingandteachingwithpraful
      @codingandteachingwithpraful 6 місяців тому

      @anuragupta I have same question in my mind also.
      Yes if we use engineType functionality it will be better instead of hasEngine.
      Thank you shreyansh sir.

  • @dpynsnyl
    @dpynsnyl Рік тому +4

    very nice explanation!
    can you also add similar video explanation for all the other principles? This is a rock solid video, I don't think I will forget this easily.

  • @SuyashD-cd9wr
    @SuyashD-cd9wr 25 днів тому +1

    Beautifully explained !!! keep up the good work !!!

  • @shubhammanecr7
    @shubhammanecr7 6 місяців тому +1

    Amazing... thank you for such clear explanation!

  • @user-wx4kx5gk1h
    @user-wx4kx5gk1h 10 місяців тому +1

    Simple and clear Explanation

  • @pavanls2436
    @pavanls2436 11 місяців тому

    One more thing that comes into mind is separation of behaviours. We can set up behaviors as interfaces and select class can implement that interface while the rest need not have to. (Learnt this through an example in Head First Design Patterns) Shreyansh Jain, correct me if I am wrong. With this, we don't have to create more classes.

  • @paritoshdadhich2954
    @paritoshdadhich2954 9 місяців тому

    Thank you so much for the wonderful explanations

  • @shubhamagarwal1434
    @shubhamagarwal1434 21 день тому +1

    Very well explained...awsome!!!

  • @shashankcool
    @shashankcool Рік тому +1

    Nicely explained with help of example 😊

  • @charuagarwal8405
    @charuagarwal8405 Рік тому +1

    Very informative video!

  • @songsbooth1329
    @songsbooth1329 3 місяці тому +1

    Superb explanation.👏👏

  • @gurupreetsingh8347
    @gurupreetsingh8347 Рік тому +7

    In side bicycle u could return false instead of null ... Why returning null .. returning false can solve the problem..

    • @ConceptandCoding
      @ConceptandCoding  Рік тому +3

      Hi Gurupreet, i want to show the example.
      If Return type is Boolean, then returning null is also a valid.
      What i wanted to showcase that these kind of scenarios Liskov principles try to avoid.
      :)

    • @gurupreetsingh8347
      @gurupreetsingh8347 Рік тому +1

      @@ConceptandCoding ok thanks

    • @beerboii2435
      @beerboii2435 Рік тому +13

      In this case, I feel a better example would be to replace hasEngine with engineType in Vehicle class. For the case of bicycle, It would be logical to return null. So it would be better to understand why it is wrong to use that way.

    • @utsavkunal5221
      @utsavkunal5221 4 місяці тому

      @@gurupreetsingh8347Just because implementation fits for that method doesn’t mean, we can have method. In Bicycle has per its behaviour there is not question of having engine, also if it is electricVehicle suppose, there you can return false.

    • @divijsharma5610
      @divijsharma5610 4 місяці тому +1

      Instead you could have turnOnEngine like in SOLID video , Cycle cannot turn on engine.

  • @charuagarwal8405
    @charuagarwal8405 Рік тому +2

    Thanks for this video!

  • @himanshukashyap1654
    @himanshukashyap1654 5 місяців тому +1

    Can we get the slides that you are sharing in your videos?

  • @arashrouhani-kalleh1797
    @arashrouhani-kalleh1797 Рік тому +2

    Amazing solution!

  • @rohitvathumilli8176
    @rohitvathumilli8176 9 місяців тому +2

    Hi Shrayansh,
    1) The vehicle class, does it follow a single responsibility principle.
    2) What if I return false instead of return null for the bicycle class, does it follow LSP

    • @paulputin
      @paulputin 9 місяців тому

      I assume that the method hasEngine() should be replaced with the method startEngine(). Then this tutorial will make sense.

    • @rohitvathumilli8176
      @rohitvathumilli8176 8 місяців тому

      Thanks , can I know whether it follows single responsibility priciple

  • @rajeshdas2295
    @rajeshdas2295 11 місяців тому +1

    Very Nice Bhaiya Got it properly!!!

  • @Explorewith4x4
    @Explorewith4x4 11 місяців тому

    Nice information brother 😊 keep sharing

  • @saiakhil9206
    @saiakhil9206 6 місяців тому +2

    I think there is a slight conceptual mistake in the start of the video.
    In the video, you mentioned that for a parent p, and child classes c1, c2, and c3, LSP says that parent *p = new c1should not break the behavior when c1 is replaced by c2, c1 is replaced by c3.
    However, my understanding is that for the same scenario, in a code, if you use c1_object.setSomething() or c2_object.setSomething() and if you have a parent object, the code should retain its behavior if you change c1_object with p1_object.
    Please correct my understanding if it's wrong.

    • @patel5532
      @patel5532 6 місяців тому

      Bro by the way, Would like to study together LLD, HLD and DSA for interview? I'm also preparing for the google.

    • @saiakhil9206
      @saiakhil9206 6 місяців тому

      Sure man@@patel5532

    • @AshishGusain17
      @AshishGusain17 6 місяців тому

      It should be reverse......the code should retain its behaviour if you change p1_object with c1_object

  • @asramogal688
    @asramogal688 11 місяців тому +1

    Hi , Even after all of the changes , in the 2nd case where Bicycle cant access hasEngine(),It still throws error right,How could we prevent this ?

  • @sachinphogat8822
    @sachinphogat8822 3 дні тому

    why to make different classes implementing interfaces. Why cant we have interface extending another inteface?

  • @codinghubfree
    @codinghubfree 2 місяці тому

    since bicycle has not engine what if you return false since its return type is boolean. will it meet Liskov principle?

    • @ConceptandCoding
      @ConceptandCoding  2 місяці тому

      i think i should name method little different which will make more sense.
      but in this example, return type is Boolean, so its a valid that NULL can be returned from this method, if it is returned, then it might break the existing code. That breaks Liskov principle

  • @mdrizwanahmed6746
    @mdrizwanahmed6746 3 місяці тому +1

    great explanation

  • @tamonashchakrabarti7909
    @tamonashchakrabarti7909 Рік тому

    Very good explanation indeed

  • @stepswithvishnu2924
    @stepswithvishnu2924 6 місяців тому

    You could have return false in place of null in case of Bicycle's hasEngine() method?

    • @ConceptandCoding
      @ConceptandCoding  6 місяців тому

      I could, but since the return type of method is 'Boolean' and null can also be returned, so that's a possible usecase.
      But yes I could have used some better method name.

  • @tarunkundhiya5196
    @tarunkundhiya5196 Рік тому +1

    Can we not return false for hasEngine in Bicycle? May be different example can be used getEngine() to show the problem. Nice explanation btw. I searched the internet most of them gives complex example without clearing much. PS: Instead of EngineVehicle we can rename MotorizedVehicle. just a thought.

    • @ConceptandCoding
      @ConceptandCoding  Рік тому

      yes, thats the example i wanted to showcase, but since its Boolean, null is the valid return value

    • @varunaggarwal7126
      @varunaggarwal7126 11 місяців тому +1

      I think, Returning null is typically used when dealing with reference types, where a variable can hold a reference to an object or have no reference (null). However, for Boolean values, the options are limited to true or false to indicate the presence or absence of a certain condition. Please correct me!

  • @vallabhchugh2075
    @vallabhchugh2075 7 місяців тому

    Great teaching. Do you have notes also ? that u can share

  • @samithanandasena6642
    @samithanandasena6642 Місяць тому

    good explanation

  • @arashrouhani-kalleh1797
    @arashrouhani-kalleh1797 Рік тому +1

    Fantastic 😍!

  • @doodo_1
    @doodo_1 6 місяців тому

    can u give slides you are using in videos for reference

  • @keshavmittal2844
    @keshavmittal2844 7 місяців тому +1

    Very well explained

  • @JustForFunYTG
    @JustForFunYTG Рік тому +2

    Why bikecycle returned NULL instead of false for hasEngines()?

    • @ConceptandCoding
      @ConceptandCoding  Рік тому

      I wanted to give an example, since method return type is Boolean (object), Null is a valid return value.

    • @learner5358
      @learner5358 11 місяців тому

      Will the problem arise if we have primitive boolean instead of Wrapper object Boolean and return false ?
      If we return false then it will follow the Liskov's substitution principle right ?

  • @shenth27
    @shenth27 24 дні тому

    I dont understand why you need to return null from hasEngine method in the Bicycle class. Shouldn't it just return false?

    • @ConceptandCoding
      @ConceptandCoding  24 дні тому

      return type is Boolean it can return null,
      but i agree method name i can take something else to explain it better

  • @learnwithme7750
    @learnwithme7750 5 місяців тому

    One question i.e I think we used multi-level inheritance to make sure that we follow strategy design pattern practises? please correct me if I am wrong.

    • @ConceptandCoding
      @ConceptandCoding  5 місяців тому

      sorry i could not understand the relation between multi-level and strategy pattern, could you pls elaborate.

    • @learnwithme7750
      @learnwithme7750 5 місяців тому

      @@ConceptandCoding sorry i think i got it wrong, thanks for addressing

  • @nikhilgupta2569
    @nikhilgupta2569 10 місяців тому +1

    Good explanation but probably the example could have been a little better for instance Bicycle's hasEngine method could simply return false in this case no need to return null. So this use-case of having Bicycle extending vehicle is not basically causing any trouble :)

    • @ConceptandCoding
      @ConceptandCoding  10 місяців тому

      Return type is Boolean, so null is a valid return type.
      Yes i think much better example can be used

  • @vaibhavkhairnar7313
    @vaibhavkhairnar7313 Рік тому +1

    very nice explanation

  • @pradipbedre653
    @pradipbedre653 Рік тому

    3:41 but why we are returning null we can return the false as well right.

    • @ConceptandCoding
      @ConceptandCoding  Рік тому +2

      Boolean is a wrapper class and null is also a valid value.
      So for example purpose i sent null.

  • @linuxadmin8027
    @linuxadmin8027 Рік тому

    Thank you❤

  • @luisgonzalez8877
    @luisgonzalez8877 7 місяців тому

    So, solution for LSP is to use ISP? In other words, use a different principal ?

  • @user-xw9eh6dh8o
    @user-xw9eh6dh8o Рік тому

    Did you provide code and this file?

  • @renudeshmukh4608
    @renudeshmukh4608 5 місяців тому +1

    Best Explanation

  • @iamkdblue
    @iamkdblue 4 місяці тому

    How do we solve above problem?
    Last Mai toh wapas conflict ho Gaya !
    Compiler error hai , solve kaise hoga wo ?
    Thanks for video aag laga diya hai 🔥

  • @shinchannohara1097
    @shinchannohara1097 Рік тому +1

    Why a boolean method returning NULL, Bicycle could have returned False instead

    • @ConceptandCoding
      @ConceptandCoding  Рік тому +2

      I wanted to show an example.
      Since it's a wrapper object, Null is a valid return type

    • @shinchannohara1097
      @shinchannohara1097 Рік тому

      @@ConceptandCoding understood

  • @rohanyadav7327
    @rohanyadav7327 4 місяці тому +1

    Isn't this looking like similar to I in Solid - interface segregation
    You are segregating the interfaces based on their functionalities

  • @novascotia2015
    @novascotia2015 11 місяців тому

    sir is this for freshers or for sed1, or sde 2. now i am working on my dsa.

  • @Aman-ln9dx
    @Aman-ln9dx 6 місяців тому

    If possible can u please provide notes ?

  • @akashchourasiya72
    @akashchourasiya72 Рік тому

    isn't also interface segregation principle?

    • @ConceptandCoding
      @ConceptandCoding  Рік тому

      Similar, but intention is different, it tries to solve problem from code which uses interface.
      Interface segregation says we should not force child class to implement method which it font require.
      But Liskov days, if we swap with other child class, code should not break

  • @user-tr8xr1un2z
    @user-tr8xr1un2z 2 місяці тому

    What is the difference between L and I then.
    Does this mean I is the solution of L??

    • @ConceptandCoding
      @ConceptandCoding  2 місяці тому +1

      While they address different concerns, Interface Segregation Principle (I) can be considered a solution to avoid violating Liskov Substitution Principle (L) by designing more cohesive interfaces.

    • @user-tr8xr1un2z
      @user-tr8xr1un2z Місяць тому

      @@ConceptandCoding Got it, Thanks😄

  • @snowleopard3925
    @snowleopard3925 3 місяці тому

    bro can I get the pdf of these notes?

  • @rohitkumarsah77
    @rohitkumarsah77 4 місяці тому

    why the bicycle is not returning false instead of null...this will not break the code.

    • @ConceptandCoding
      @ConceptandCoding  4 місяці тому

      return type of the method is Boolean, so returning Null is a valid return type.i think method can be improved a bit

  • @chandrikasaha6301
    @chandrikasaha6301 8 місяців тому

    sir if you could share a git link. I am trying these in python

  • @sreelekshmiraj6424
    @sreelekshmiraj6424 Рік тому

    Could you please provide the codes or notes pdf

  • @Jock3R87
    @Jock3R87 Рік тому

    In bicycle class you are returning nil ideally it should return false.. i guess this is not the correct example

    • @ConceptandCoding
      @ConceptandCoding  Рік тому

      Return type is Boolean, null is a valid return value.

    • @Jock3R87
      @Jock3R87 Рік тому

      @@ConceptandCoding in any of the programming language Bool is not have null value mate.. Its either True/ False otherwise the whole meaning of Bool is wasted

    • @ConceptandCoding
      @ConceptandCoding  Рік тому

      @@Jock3R87 welcome to Java world buddy, it's a wrapper object and null is valid value for it.
      Yes generally it make sense to return only true false but since we define the return type as Boolean object not the primitive one boolean. , Null can be passed. Hope this clarifies

  • @Aman-ln9dx
    @Aman-ln9dx 6 місяців тому

    Your slide

  • @AbhishekKumar-kk6qs
    @AbhishekKumar-kk6qs 10 місяців тому

    Hi Shreyansh, So the solution is strategy design pattern , am i right ?

    • @ConceptandCoding
      @ConceptandCoding  10 місяців тому +1

      :) no Abhishek, i would say, interface segregation generally solves this