This is basic question but still i don't understand encapsulation concept . I did't understand how can we change the properties of class from other class.because whenever we try to set the public instance value of class we have to create object of that class and then set the value.and every object refer to different memory.so even if we change the instance value this will not impact to any other object.
Even I try to change using static public instance value also i am not able to change the class property value.
Example is given below
// Employee class
public class Employee {
public static int empid;
public static String empname;
public static void main(String[] args) {
System.out.println("print employe details:"+empid+" "+empname);
}
// EmployeeTest class
public class EmployeeTest {
public static void main(String[] args) {
Employee e = new Employee();
e.empid=20;
e.empname="jerry";
Employee.empid=10;
Employee.empname="tom";
}
}
}
Every time I run Employee
class I am getting same value
print employe details:0 null
Even though I am not following encapsulation concept and I am not able to change public instance value of employee class.Please help me to understand the concept where i am going wrong.
Yeah, this can be a little confusing sometimes. Let's go step by step: First, you need to understand
Encapsulation is one of the four fundamental OOP concepts.Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods. If a field is declared private, it cannot be accessed by anyone outside the class, thereby hiding the fields within the class. For this reason, encapsulation is also referred to as data hiding.
Encapsulation can be described as a protective barrier that prevents the code and data being randomly accessed by other code defined outside the class. Access to the data and code is tightly controlled by an interface.
The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code. With this feature Encapsulation gives maintainability, flexibility and extensibility to our code.
Take a small example:
The above methods are called Accessors(aka getters and setters). Now you might ask,
For instance, consider a field public in a class which is accessed by other classes. Now later on, you want to add any extra logic while getting and setting the variable. This will impact the existing client that uses the API. So any changes to this public field will require change to each class that refers it. On the contrary, with accessor methods, one can easily add some logic like cache some data, lazily initialize it later. Moreover, one can fire a property changed event if the new value is different from the previous value. All this will be seamless to the class that gets value using accessor method.
There are so many tutorials and explanations as to how and what are they. Google them.
As for your, current problem:
In the meantime, read up on
for a better understanding of the concepts. Hope it helps. :)
The reason you are getting the output "print employe details:0 null" when running the Employee class is because those variables are not initialized. That is, you do not assign any values to them within the Employee class.
Whatever you do within the EmployeeTest class will not affect the values of the variables in Employee the next time it is run. Consider each run of a Java program a "clean slate".
On the point of encapsulation, you really should not be using the
static
keyword. If you are going for encapsulation check out the other answer to this question, it has a nice code sample for you to use.The concept of encapsulation is a design technique that relates with information hiding. The underlying principle is to provide protected access to the class attributes, through a well designed interface. The purpose of encapsulation is to enforce the invariants of the class.
To follow on your example consider the interface of this class:
Note that by declaring the attributes as private this class restricts clients from directly accessing the state of employee object instances. The only way for clients to access them is via the getName() method. This means that those attributes are encapsulated by the class. Also note that by declaring the attributes as final and initializing them in the constructor we create an effectively immutable class, that is one whose state cannot be modified after construction.
An alternative implementation would be the following:
In this example the object is not immutable but its state is encapsulated since access to it takes place only through accessors and modifiers. Note here how encapsulation can help you protect the invariants of the object state. By constraining modifications through a method you gain a better control of how the object state is modified, adding validations to make sure that any modifications are consistent with the specification of the class.
encapsulation =VARIABLES(let private a,b,c)+ METHODS(setA&getA,setB&getB....) we can encapsulate by using the private modifier. let consider your created one public variable and one private variable in your class... in case if you have to give those variables to another class for read-only(only they can see and use not able to modify) there is no possibility with public variables or methods,but we can able to do that in private by providing get method. so Your class private variables or methods are under your control. but IN public there is no chance....i think you can understood.
Encapsulation can be described as a protective barrier that prevents the code and data being randomly accessed by other code defined outside the class. Access to the data and code is tightly controlled by an interface.
Encapsulation in Java is the technique of making the fields in a class private and providing access to the fields via public methods.
If a field is declared private, it cannot be accessed by anyone outside the class, thereby hiding the fields within the class. For this reason, encapsulation is also referred to as data hiding.
Real-time example: cars and owners. All the functions of cars are encapsulated with the owners. Hence, No one else can access it..
The below is the code for this example.
ENCAPSULATION is mechanism of wrapping methods and variables together as a single unit. for example capsule i.e. mixed of several medicines.
variables of a class will be hidden from other classes as it will be declared private, and can be accessed only through the methods of their current class
To achieve encapsulation in Java − * Declare the variables of a class as private. * Provide public setter and getter methods to modify and view the variables values. * The Java Bean class is the example of fully encapsulated class.