Five rules to follow for a good system design → Low-level design

1. Minimize the accessibility of classes and members

  • Instance fields of public classes should be made rarely public
  • If a method overrides a superclass method, it cannot have a more restrictive access level in the subclass than in the superclass
  • Never make mutable fields final
public static final Thing[] VALUES = { ... };
//It's potential security hole in the

2. In a public class never use public fields

// Degenerate classes like this should not be public!
class Point {
public double x;
public double y;

Always use accessor use fields in public class

// Encapsulation of data by accessor methods and mutators
class Point {
private double x;
private double y;

public Point(double x, double y) {
this.x = x;
this.y = y;

public double getX() { return x; }
public double getY() { return y; }

public void setX(double x) { this.x = x; }
public void setY(double y) { this.y = y; }

3. Minimize mutability

The major disadvantage of immutable classes is that they require a separate object for each distinct value.

To make a class immutable, follow these five rules:

  • Don’t provide methods that modify the object’s state (known as mutators).
  • Ensure that the class can’t be extended.
  • Make all fields final.
  • Make all fields private.
  • Ensure exclusive access to any mutable components.

4. Favor composition over inheritance

Unlike method invocation, inheritance violates encapsulation . In other words, a subclass depends on the implementation details of its superclass for its proper function. The superclass’s implementation may change from release to release, and if it does, the subclass may break, even though its code has not been touched.

When you are extending class have few implementations in one class and other parts of implementation in another class.

5. Prefer interfaces to abstract classes

Software engineer, Leading highly scalable product @scale of million records per day.