Liskov Substitution Prensibi Nedir ? (Kod örneğiyle) — SOLID

Liskov substitution yazılım prensibi
public class Square extends Rectangle {    @Override
public void setWidth(int width) {
super.setWidth(width);
super.setHeight(width);
}
@Override
public void setHeight(int height) {
super.setHeight(height);
super.setWidth(height);
}
}
@Test
public void testRectangleArea() throws Exception {
Rectangle rectangle = new Square();
rectangle.setWidth(20);
rectangle.setHeight(4);
assertEquals(80, rectangle.area());
}
public interface Shape {
long area();
}
public class Square implements Shape {private int size;public Square(int size) {
this.size = size;
}
@Override
public long area() {
return size * size;
}
public void setSize(int size) {
this.size = size;
}
}
public class Rectangle implements Shape {    private int width;
private int height;
public Rectangle(int width, int height) {
this.width = width;
this.height = height;
}
@Override
public long area() {
return width * height;
}
public void setWidth(int width) {
this.width = width;
}
public void setHeight(int height) {
this.height = height;
}
}
    @Test
public void testRectangleArea() throws Exception {
Shape rectangle = new Rectangle(10, 5);
assertEquals(50, rectangle.area());
}
@Test
public void testSquareArea() throws Exception {
Shape square = new Square(5);
assertEquals(25, square.area());
}

--

--

--

Software Engineer @Yemeksepeti • #Java • #Spring Boot • #Kotlin • #Spark • #Microservices • https://gokhana.dev

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Gökhan Ayrancıoğlu

Gökhan Ayrancıoğlu

Software Engineer @Yemeksepeti • #Java • #Spring Boot • #Kotlin • #Spark • #Microservices • https://gokhana.dev

More from Medium

CS371p Spring 2022: Sarunas Budreckis — Entry #9

Understanding Inversion Of Control & Dependency Injection

My Review of Spring: Spring MVC

Spring boot application & angular front-end