Solved

what is Liskov's Substitution Principle

Posted on 2014-12-17
2
105 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
github account with ecipse 1 48
HSSFWorkbook cannot be resolved error 10 72
web services creation SOAP vs REST 5 42
Convert from a json string array to a Java object 3 36
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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 scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

831 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