-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathLiskovPrincipleLesson.java
More file actions
62 lines (48 loc) · 1.14 KB
/
Copy pathLiskovPrincipleLesson.java
File metadata and controls
62 lines (48 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package solid;
public class LiskovPrincipleLesson {
private static Rect getNewRectangle()
{
//some factory mehtod
return new Square();
}
public static void main (String args[])
{
Rect r = LiskovPrincipleLesson.getNewRectangle();
r.setWidth(5);
r.setHeight(10);
System.out.println(r.getArea());
}
}
//Liskov substitution principle
//if S is a subtype of T, then objects of type T may be replaced with objects of type S without altering any of the desirable properties of T
class Rect
{
protected int width;
protected int height;
public void setWidth(int width){
this.width = width;
}
public void setHeight(int height){
this.height = height;
}
public int getWidth(){
return width;
}
public int getHeight(){
return height;
}
public int getArea(){
return width * height;
}
}
class Square extends Rect
{
public void setWidth(int width){
this.width = width;
height = width;
}
public void setHeight(int height){
width = height;
this.height = height;
}
}