Liskov’s Substitution Principle(LSP)

12 Sep

Problem explained at here : http://www.oodesign.com/liskov-s-substitution-principle.html

Solution
———-

1. CREATE RECTANGLE class

package lsp;

/**
* @author kumarash
*
*/
public class Rectangle {

protected int hight;
protected int width;

/**
*
*/
public Rectangle() {

// TODO Auto-generated constructor stub
}

public final int getArea() {

return hight * width;
}

/**
* @return the hight
*/
public final int getHight() {

return hight;
}

/**
* @param hight
* the hight to set
*/
public final void setHight(int hight) {

this.hight = hight;
}

/**
* @return the width
*/
public final int getWidth() {

return width;
}

/**
* @param width
* the width to set
*/
public final void setWidth(int width) {

this.width = width;
}
}

2. CREATE Square class as

package lsp;

/**
* @author kumarash
*
*/
public class Square extends Rectangle {

/**
*
*/
public Square() {

// TODO Auto-generated constructor stub
}

public void setHightWidth(int hightWidth) {

this.hight = hightWidth;
this.width = hightWidth;
}

}

3. Now test it , must be follow lsp !!!

package lsp;

/**
* @author kumarash
*
*/
public class lspTest {

/**
*
*/
public lspTest() {

// TODO Auto-generated constructor stub
}

/**
* @param args
*/
public static void main(String[] args) {
final Rectangle rectangle=new lspTest().rectangleFactory();
rectangle.setHight(3);
rectangle.setWidth(6);
System.out.println(rectangle.getArea());
}

public Rectangle rectangleFactory() {

return new Square();
}

}

thank you

Advertisements

Answer Please

27 Jan

Does Process oriented (Procedural) program is much faster than OOps program?

Please give your comments with why?

Java Tuning Tips

27 Jan

Hello friend,

I am too excited to writing this blog in behalf of java programming improvement. I hope to see all java coding should be more efficient, robust and performance oriented. I mooted some tips how we make our code more efficient.

1. Use turnary operator “?” whenever possible.

2. Minimize the use of Casting in your code. Casting is too overhead on JVM.

3. Use Short Circuit Boolean operator

4. Do not rely on finalize method it may cause your resource may not free at expected time.

5. Make your method final, if it will not going to override. It gives you early binding in your generated byte code.

6. Minimize to declare Static variables / constant in a class, that variables not include in gc. Its memory allocation not re-available until the class is unloaded.

Keep reading…………..
Thank You