Solved

what is Liskov's Substitution Principle

Posted on 2014-12-17
2
106 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
2 Comments
 
LVL 11

Accepted Solution

by:
Tej Pratap Shukla ~Dexter earned 500 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

828 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