Encapsulation in Java

Google 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

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 Advertisements

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

Pure VPN Privide Lowest Price VPN Just @ $1.65. Per Month with Non Detected IP Lowest Price Non Detected IP VPN

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

Download Projects

Google Advertisements


Buy Websites 9999595223

Buy Fresh and Payment Receive Media.net Account with Website. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs