Encapsulation

Encapsulation is a fundamental of Object Oriented Programming (OOP). I have used this fundamental many times before and thought nothing of it. However when I was asked this question I totally forgot about it. So I decided to write about it for both you and I to better understand it.

In basic terms encapsulation is a technique of making objects in a class public or private. For example a private object cannot be accessed outside the current class. The technique may also be referred to as data hiding. If you would like to provide a method or object access outside the current class then you need to declare it as public. This will allow other classes to access this method and use its resources.

Encapsulation can be used as a protective barrier against other code being accessed outside the class randomly by other code.

The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code. With this feature Encapsulation gives maintainability, flexibility and extensibility to our code.

Now let’s take a look into the code that is involved with encapsulation.

/*EncapTest.java (FILE NAME)*/
public class EncapTest{

private name;

public String getName(){
          return name;
}

public void setName(String newName){
          this.name = newName;
     }
}

The object \”name\” is private and can only be accessed through this class. However we have set the methods to public which will allow other classes to either set or get the object name. These methods are referred to as setters and getters. Let’s take a look at how to access these public methods outside this class.

/* RunEncapTest.java (FILE NAME)*/
public class RunEncapTest{

public static void main(String args[]){

EncapTest encap = new EncapTest();
encap.setName("Brandon");
System.out.print("Name: " + encap.getName());

}
}

//If you run the code it will display: Name: Brandon

Benefits of Encapsulation: The fields of a class can be made read-only or write-only.  A class can have total control over what is stored in its fields. The users of a class do not know how the class stores its data. A class can change the data type of a field, and users of the class do not need to change any of their code.

About bwilson 37 Articles
Mobile device specialist currently working as a system analyst, building out an IT infrastructure. I currently focus much of my work on relational databases as well as frameworks. The goal of my work is to continually improve processes and efficiencies.

1 Comment

  1. Hello, its pleasant piece of writing concerning
    media print, we all know media is a impressive source
    of data.This creates potential for a glaring lack of artistry
    and musical cohesiveness between the vocals on a song and its underlying instrumental.
    For data recovery New York-based options can provide instant quotes.
    Because the Cleanroom is larger than average, it is
    officially tested every three months to ensure consistency.

Leave a Reply

Your email address will not be published.


*