Java Encapsulation

Encapsulation is a concept in object oriented programming (OOP). It is nothing new and it is as popular as Harry Potter books among kids. I wrote a tutorial on relationships, like association, aggregation, composition, abstraction, etc and it was well received. I have been planning for long to write about all the OOPs concepts and this tutorial on encapsulation is one among that series.
Why is it titled Java encapsulation? Do we have something special for encapsulation in java? Let us be clear, encapsulation is a OOP concept and we will see an example of how it is implemented in Java. The key concepts in OOP are abstraction, encapsulation, inheritance and polymorphism and we will have a separate tutorial for each of these OOP concepts.

Encapsulation Definition

Encapsulation is the ability to package data, related behavior in an object bundle and control/restrict access to them (both data and function) from other objects. It is all about packaging related stuff together and hide them from external elements.
We can see that keywords encapsulation and data hiding are used synonymously everywhere. It should not be misunderstood that encapsulation is all about data hiding only. When we say encapsulation, emphasis should be on grouping or packaging or bundling related data and behavior together.


When we design a class in OOP, the first principle we should have in mind is encapsulation. Group the related data and its behavior in a bucket. Primary benefit of encapsulation is, better maintainability.
That’s it! This is all about encapsulation. Let us leave it as simple as that. Nothing more and nothing less.

Encapsulation in Java

Any well defined Java class in its context domain is an example for encapsulation in Java. So the importance here is for bundling data and methods together only after that data hiding comes into picture. When we say data hiding, we all know about access specifiers in Java. Hiding an attribute or method is easier from the outer world. Just use the access specifier ‘private’.
So data hiding is fairly straight forward. How about bundling data and method. This is critical. We should understand well about the business domain and based on that design the class and group attributes and its methods together. This process is key in encapsulation.
Following is an outline example for Java encapsulation. When we talk about an animal, we should have all its attributes listed, similarly its behavior like how it will hunt, run, mate, etc. By bundling all these data and behavior in a single class we are following encapsulation principles.
 
 
package java;
 
public class Animal {
  private String animalName;
  private String animalType;
  private int height;
  private String color;
 
  public Animal(String animalName, String animalType) {
    this.animalName = animalName;
    this.animalType = animalType;
  }
 
  public void hunt() {
    // implementation of hunt
  }
 
  public void run() {
    // implementation of run
  }
 
  public void mate() {
    // implementation of mate
  }
  //encapsulation is not about having getter/setters
  public String getAnimalName() {
 
    return animalName;
  }
 
  public void setAnimalName(String animalName) {
    this.animalName = animalName;
  }
 
  public String getAnimalType() {
    return animalType;
  }
 
  public void setAnimalType(String animalType) {
    this.animalType = animalType;
  }
 
}



0 comments:

Post a Comment