Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

what is Liskov's Substitution Principle

Posted on 2014-12-17
2
Medium Priority
?
128 Views
Last Modified: 2015-02-10
Hi,
Please explain me the Liskov's Substitution Principle. Whats the use and why its important to follow this principle.

Thanks
0
Comment
Question by:Rohit Bajaj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 12

Accepted Solution

by:
Tej Pratap Shukla ~Dexter earned 1500 total points
ID: 40506437
Hello..

According to Linkov's Principle, if a program module is using a Base class, then the reference to the Base class can be replaced with a Derived class without affecting the functionality of the program module.

Example of Likov's Substitution Principle

class rectangle
{
      protected int widthof;
      protected int heightof;

      public void setWidth(int width){
            widthofm = width;
      }

      public void setHeight(int height){
            heightof = height;
      }


      public int getWidth(){
            return widthofm;
      }

      public int getHeight(){
            return heightofm;
      }

      public int getArea(){
            return widthofm * heightofm;
      }      
}

class Square extends Rectangle
{
      public void setWidth(int width){
            widthofm = width;
            heightofm = width;
      }

      public void setHeight(int height){
            widthofm = height;
            heightofm = height;
      }

}

class LspTest
{
      private static Rectangle getNewRectangle()
      {
             
            return new Square();
      }

      public static void main (String args[])
      {
            Rectangle r = LspTest.getNewRectangle();
       
            r.setWidth(5);
            r.setHeight(10);
            

            System.out.println(r.getArea());
      }
}
0
 

Author Comment

by:Rohit Bajaj
ID: 40506650
I didnot understand this example clearly.
why will someone implement a function like :
private static Rectangle getNewRectangle()
      {
             
            return new Square();
      }

when we want a rectangle. this looks like a coding error
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.
Suggested Courses

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question