Skip to main content

Liscov Substitution principle

Liscov Substitution Principle 

Inheritance is one of the important OOP concept. Liscov Substitution Principle is applied when using inheritance in our program. We must make sure that the child classes just extend without replacing the functionality of parent class. Otherwise the child classes can produce undesired effects when they are used in existing program modules.
child component must be completely substitutable for their parent components
.
As an example if we take the classes " Rectangle" and "Square"

In mathematics square is also a kind of rectangle. which makes the square class a child of rectangle class. rectangle class has some methods and they might be over ridden by the square class.


if we have a function draw() and we do not know what shape will be returned but it will surely be a rectangle. If we give the length = 10 cm and width = 5 cm


violating the Liscov Substitution principle is when the first length entered will be taken as the length of the square and returning a square of area 10 cm X 10 cm instead of a rectangle of area 10 cm X 5 cm


Violation of Likov's Substitution Principle



class Rectangle
{
protected int m_width;
protected int m_height;

public void setWidth(int width){

m_width = width;
}

public void setHeight(int height){

m_height = height;
}


public int getWidth(){

return m_width;
}

public int getHeight(){

return m_height;
}

public int getArea(){

return m_width * m_height;
}
}

class Square extends Rectangle

{
public void setWidth(int width){
m_width = width;
m_height = width;
}

public void setHeight(int height){

m_width = height;
m_height = height;
}

}


class LspTest

{
private static Rectangle getNewRectangle()
{
// it can be an object returned by some factory ...
return new Square();
}

public static void main (String args[])

{
Rectangle r = LspTest.getNewRectangle();
       
r.setWidth(5);
r.setHeight(10);
// user knows that r it's a rectangle.
// It assumes that he's able to set the width and height as for the base class

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

// now he's surprised to see that the area is 100 instead of 50.
}
}

If the square class looks similar to rectangle but when we do some action if we don't get the expected out put we probably have the wrong abstraction there.




Comments

Popular posts from this blog

MongoDB Up & Running

MongoDB Up & Running What is mongoDB ?             Mongo DB has rapidly grown to become a popular database for web applications and is a perfect fit for Node.JS applications, letting you write Javascript for the client, backend and database layer. Its schemaless nature is a better match to our constantly evolving data structures in web applications, and the integrated support for location queries is a bonus that’s hard to ignore. Throw in Replica Sets for scaling, and we’re looking at really nice platform to grow your storage needs now and in the future. MongoDB is an   open source   database that uses a document-oriented data model.MongoDB is one of several   database   types to arise in the mid-2000s under the   NoSQL   banner. Instead of using   tables   and   rows   as in   relational databases, MongoDB is built on an architecture of collections and documents. Do...

Apache Maven

Introduction to Apache Maven   What is maven? Maven is a project management tool which encompasses a project object model, a set of standards, a project life cycle, a dependency management system, and logic for executing plugin goals at defined phases in a life cycle. When you use Maven, you describe your project using a well-defined project object model, Maven can then apply cross-cutting logic from a set of shared (or custom) plugins. The great majority of Maven users are going to call Maven a “build tool”: a tool used to build deployable artifacts from source code. Build engineers and project managers might refer to Maven as something more comprehensive: a project management tool. What is the difference? A build tool such as Ant is focused solely on preprocessing, compilation, packaging, testing, and distribution. A project management tool such as Maven provides a super set of features found in a build tool. In addition to providing build capabilities, Maven can als...

Tour Management System - 2nd year ITP project

   Introduction to the System  The Lotus Tours Company which is located in Nugegoda, provides a good service to the customers who wish to visit sacred places in India and Thailand. India and Thailand are some beautiful countries in Asia. A trip to these countries can reveal numerous mystic things regarding its culture, art tradition history etc. Any person who wish to travel through the Lotus Company are allowed. Even the customers who doesn’t have a passport can reserve a date and get registered. Company will get all the details from the customer who doesn’t have a passport and help to prepare a new passport for them They have many packages in different prices. To get registered every customer should deposit Rs. 10,000 in advance. Then the rest of the amount are paid in installments prior to the tour date or the full amount for the tour can be paid at once. When a customer make a reservation, all the reservation details are recorded in a file and unique ID numbe...