Solved

what is Liskov's Substitution Principle

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
changePi Challenge 15 77
network + 7 73
PDFBox - convert image to PDF, PDF resolution 3 70
Cipher Configuration on Apache HTTPD 4 24
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.

747 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now