Solved

what is Liskov's Substitution Principle

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

710 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