Encapsulation in Java

Advertisements

Abstraction in Java Polymorphism in Java

Real Life Example of Encapsulation in Java

Encapsulation is a process of wrapping of data and methods in a single unit is called encapsulation. Encapsulation is achieved in java language by class concept.

Combining of state and behavior in a single container is known as encapsulation. In java language encapsulation can be achieve using class keyword, state represents declaration of variables on attributes and behavior represents operations in terms of method.

encapsulation in java

Advantage of Encapsulation

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

Real life example of Encapsulation in Java

The common example of encapsulation is capsule. In capsule all medicine are encapsulated in side capsule.

real life example of encapsulation

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.

Let's see the Example of Encapsulation in java

Example

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

Role of Access Specifiers in Encapsulation

encapsulation in java

Access specifiers plays an important role in implementing encapsulation in C++. The process of implementing encapsulation can be sub-divided into two steps:

  • The data members should be labeled as private using the private access specifiers.
  • The member function which manipulates the data members should be labeled as public using the public access specifier.

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 set the value of data member and get function get the value of data member.

Advantage of Encapsulation in C++

  • The main advantage of using of encapsulation is to secure the data from other methods, when we make a data private then these data only use within the class, but these data 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 an illegal direct access.
  • Encapsulation also helps us to make a flexible code which is easy to change and maintain.

Dis-Advantage of Encapsulation in C++

You can't access private date outside the class.

Designing Strategy of Encapsulation in C++

Most of us have learnt to make class members private by default unless we really need to expose them. That's just good encapsulation. This is applied most frequently to data members, but it applies equally to all members, including virtual functions.


Abstraction in Java Polymorphism in Java

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements