Minhaj Mehmood bio photo

Minhaj Mehmood

Full Stack Developer

Email Twitter Google+ LinkedIn Tumblr Github

No Coupling is not a part of encapsulation, coupling use encapsulation, where the well-encapsulation can be achieved without loose coupling. For example, I’ve a DSLR Camera(MyCamera) and two lenses of Different types which i switch between quite often as per my photography shoots requirement.

So lets say I have a Camera MyCamera & having 1 lens to take wide angle pictures and 2nd to take really close photos of any object.

Check the following code:

interface Lens { }

class MacroLens implements Lens { }

class WideAngleLens implements Lens { }

public class MyCamera{
  private Lens lens;
  MyCamera (Lens lens) {
    this.lens = lens;
  }
}

The above code demonstrate well-encapsulation + loose coupling with each other. I can switch lenses easily from Wide Angle Lens to Macro Lens. If I were use following code:

public class MyCamera{

private WideAngleLens lens;
  MyCamera(WideAngleLens lens){
    this.lens = lens;
    }

}

original post