-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathI.java
64 lines (53 loc) · 1.88 KB
/
I.java
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
63
64
/*
The design principle used in the provided code is the Interface Segregation
Principle (ISP). This code also illustrates OCP.
The Interface Segregation Principle states that clients should not be
forced to depend on interfaces they do not use. It suggests that you
should break down interfaces into smaller, specific ones so that classes
implementing those interfaces are not forced to implement methods they
don't need.
In the provided code:
The Shape interface declares a single method getArea(), which is a
minimalistic approach to defining the behavior common to all shapes.
The Rectangle and Square classes implement the Shape interface, but they
only provide implementations for the getArea() method, which is relevant
to their respective shapes.
By adhering to the Interface Segregation Principle, the code ensures that
each class implements only the methods it needs, keeping the interfaces
cohesive and the classes focused on their specific responsibilities.
*/
interface Shape {
float getArea();
}
class Rectangle implements Shape {
private float length;
private float width;
public Rectangle(float length, float width) {
this.length = length;
this.width = width;
}
@Override
public float getArea() {
return length * width ;
}
}
class Square implements Shape {
float side;
public Square (float side){
this.side = side;
}
@Override
public float getArea(){
return side * side;
}
}
public class I {
public static void main(String [] arg){
Rectangle R = new Rectangle(4, 5);
float rectangleArea = R.getArea();
System.out.println("Area of Rectangle : " + rectangleArea);
Square S = new Square(4);
float SquareArea = S.getArea();
System.out.println("Area of Square : "+ SquareArea);
}
}