Encapsulation in Java
Encapsulation is one of the fundamental concepts of object-oriented programming (OOP). The idea of grouping data and methods that work on this data in a unit is described, for example, a class in Java.
This concept is also frequently used to hide the internal representation or state of an object on the outside. This is called information concealment. The general idea of this mechanism is simple. If you have an attribute that is not visible on the outside of an object and package it with methods that provide read or write access to it.
If you know any object-oriented programming language, you probably know that these methods are getter and setter methods.Depending on the methods implemented, you can decide if an attribute can be read and changed, if it is read-only or if it is not visible. As shown later, you can also use the setter method to implement additional validation rules to ensure that the object always has a valid state.
Let’s take a look at an example that shows the concept of encapsulation and how it can be used to implement information hiding and apply additional validation before changing the values of its object attributes.
The concept of encapsulation is to keep the implementation (code) and the manipulated data (variables) together. Sufficient encapsulation ensures that code and data are protected against misuse by external entities.
Information hiding in Java
As explained at the beginning, you can use the concept of encapsulation to implement an information concealment mechanism. Like the concept of abstraction, this is one of the most used mechanisms in Java. You can find examples of it in almost all Java classes well implemented.
You implement this information hiding mechanism, making your inaccessible class attributes out and providing getter and / or setter methods for attributes that must be readable or updatable by other classes.
Interested in learning Java? Enroll now:” Java training in Chennai “
Encapsulation is one of the main concepts of object-oriented programming. It describes the grouping of data and methods that operate with this data in a unit.
It is normally used to implement an information concealment mechanism. This mechanism reduces the accessibility of attributes to the current class and uses public getter and setter methods to control and restrict external access to these attributes. These methods not only allow you to define which attributes can be read or updated, but also allow you to validate the new value before changing the attribute.
To getting expert-level training for Java Training in your location – java training in Chennai | java training in Bangalore | java training in Pune | java training in Chennai | java training in Bangalore | java training in Chennai | java interview questions and answers | core java interview questions and answers | java training in Chennai | For getting java online training | java online training