What does the term encapsulation mean?

Asked By 10 points N/A Posted on -
qa-featured

Hey Friend,

What does the term encapsulation mean and what is its function in Java?

Can anyone explain me details about it?

Thanks.

 

SHARE
Best Answer by Schneider Matthew
Answered By 0 points N/A #101936

What does the term encapsulation mean?

qa-featured

Hey pal,

Encapsulation looks like a hard and complicated word, but guess what it is very easy to understand it. It means the idea of hiding given data but making it only available using a given specified method. The use of this in java is to ensure that there are minimal chances of trying to change anything in Java by mistake. Additionally it helps in Java be capable of accessing these hidden data and other specifies. The Java is also capable of accessing the classes, the fields, and also the methods and the constructors by the utilization of the modifiers.

Hope this will help you.

Regards!

Best Answer
Best Answer
Answered By 0 points N/A #195492

What does the term encapsulation mean?

qa-featured

Hello Samantha Smith,

Encapsulation or Data hiding is one of the most important feature of Java. It is one of the OOP properties Java posses. It is nothing but keeping the variables inside a class hidden from outside the class. The only access to these variables is through specific functions inside the class. Please find the below example which may help you understand the problem easily.
 
 
public class TestEncapsulation{
 
   private String name;
   private int age;
 
   public String getName(){
      return name;
   }
   public int getAge(){
      return age;
   }
 
   public void setName(String tempName){
      name = tempName;
   }
 
   public void setAge( int tempAge){
      age = tempAge;
   }
}
 
 
The variables here are declared as private hence it will not be visible outside the class. Public methods are defined which is used to manipulate these variables.
 
Now we will access these functions from another class.
 
public class TestRunEncapsulation{
 
   public static void main(String args[]){
      TestEncapsulation testEncapsulation = new TestEncapsulation();
      testEncapsulation.setName("John");
      testEncapsulation.setAge(25);
 
      System.out.print("Name : " + testEncapsulation.getName()+ 
                             " Age : "+ testEncapsulation.getAge());
    }
}
 
The following will be the output of the program.
 
Name : John Age : 25
 
Hope it helps.

Login/Register to Answer

Related Questions