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

An Introduction to Spring Framework

An Introduction to Spring Framework   What is Spring ? Spring is an application framework . Unlike single-tier frameworks such as Struts or Hibernate, Spring aims to help structure whole applications in a consistent, productive manner, pulling together best-of-breed single-tier frameworks to create a coherent architecture. Why Spring ? The Spring Framework is an open source application framework that aims to make J2EE development easier. We’ll look at the motivation for Spring, its goals, and how Spring can help you develop high-quality applications quickly. Using J2EE “out of the box” is not an attractive option. Many J2EE APIs and services are cumbersome to use. J2EE does a great job of standardizing low-level infrastructure, solving such problems as how can Java code access transaction management without dealing with the details of transactions. But J2EE does not provide an easily usable view for application code.That is the role of an application framework, such a

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 also ru

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 number is given to