The Interface Segregation Principle states that:
A client should not be forced to depend on methods it does not use.
This means that:
For example, we may have an abstract class Shape with methods to get shape properties as well as scale a shape.
abstract class Shape {
abstract double getArea();
abstract double getPerimeter();
abstract Shape scale(double factor);
}
However, this would violate the Interface Segregation Principle as some classes might want to implement scale but are unable to implement getArea and getPerimeter (e.g. 3D Objects). In addition clients might need to implement getArea and getPerimeter without needing to implement scale.
It would be better to separate the the methods dealing with shapes properties from methods dealing with scaling into different interfaces to avoid violating the Interface Segregation Principle.
interface Scalable {
Scalable scale(double factor);
}
abstract class Shape {
abstract double getArea();
abstract double getPerimeter();
}
This way, shapes can choose to implement scalable only if needed.
class Circle extends Shape implements Scalable {…}