Encapsulation
 is a process of wrapping data and methods in a single unit is called encapsulation. Encapsulation is achieved in java language by the class concept.The combining of state and behavior in a single container is known as encapsulation. In java language encapsulation can be achieved using the class keyword, the state represents a declaration of variables on attributes and behavior represents operations in terms of method. 

Advantage of Encapsulation

The main advantage of using encapsulation is to secure the data from other methods, when we make data private then this data only use within the class, but this data is not accessible outside the class.

Real-life example of Encapsulation in Java

A common example of encapsulation is a capsule. In capsule, all medicine is encapsulated in a side capsule.


Benefits of encapsulation

  • Provides abstraction between an object and its clients.
  • Protects an object from unwanted access by clients.
  • Example: A bank application forbids (restrict) a client to change an Account's balance.                                                                                                                                                                                                    

    How Encapsulation is achieved in a class
    • Make all the data members private.
    • Create public setter and getter functions for each data member in such a way that the set function sets the value of the data member and the get function gets the value of the data member.

    Advantage of Encapsulation 
    • The main advantage of using encapsulation is to secure the data from other methods, when we make data private then this data only use within the class, but this data is not accessible outside the class.
    • The major benefit of data encapsulation is the security of the data. It protects the data from unauthorized users that we inferred from the above stated real-real problem.
    • We can apply the concept of data encapsulation in the marketing and finance sector where there is a high demand for security and restricted access of data to various departments.
    • Encapsulation helps us in binding the data(instance variables) and the member functions(that work on the instance variables) of a class.
    • Encapsulation is also useful in hiding the data(instance variables) of a class from illegal direct access.
    • Encapsulation also helps us to make a flexible code that is easy to change and maintain.


Program:

class Employee { private String name; public String getName() { return name; } public void setName(String name){ this.name=name; } } class Demo { public static void main(String[] args) { Employee e=new Employee(); e.setName("Harry"); System.out.println(e.getName()); } }

Output:

Harry

  • Progamming is what actually means ?
  • What is software development ?
  • How we categorized software development in different manner.
C Programming
C Plus Plus
Python
Java Development
Web Designing
Javascript
Mysql
Oracle
We cover all the programming concepts in various programming languages, this tutorials are very help full for bigener as well as Experience developer, for the ease of understanding we categorized programming in different manner likewise.