Let's try to generate the need of encapsulation.
For an instance, When money is in the Bank, It is secured. Bank takes responsibility and provides security for our money.
But If we have withdrawn that and carried in a bag, Now It can be snatched. Same happens, when we create an application. The database takes responsibility and provides security to our data. But when our application access data from the database and stores in the variable, Now It can be theft.
Data should not travel openly. Must have a container to wrap that same as In the real world(from this point you should know that how a real-world object will be defined in a program),
If I am an object of the Human class, with variables
Nobody can know my name by just looking at me without asking. So now name became secured. To wrapping up data like this in a class is called Encapsulation.
In code:
Even data became secured from other objects also. For an instance, If I will workout, I'll get the Muscular body, not my neighbor. In the same way, If one object changed it's variable's value, the variable will not be changed for any other objects except this. i.e variables and functions of one object cannot be accessed by other objects, and it just because of Encapsulation.
for example(in code):
class Human{
var name;
var height;
var color;
var weight;
public Human(String name, float height, String color, float weight){
name = name;
height = height;
color = color;
weight = weight;
}
public void workout(){
weight = weight + 5;
}
public static void main(String[] args){
Human object1 = new Human("foo", 5.7, "fair", 70);
Human object2 = new Human("bar", 5.9, "fair", 70);
object1.workout();
// object1 did workout, object2 not
System.out.println(object1.weight) // 75
System.out.println(object2.weight) // 70
}
}
For an instance, When money is in the Bank, It is secured. Bank takes responsibility and provides security for our money.
But If we have withdrawn that and carried in a bag, Now It can be snatched. Same happens, when we create an application. The database takes responsibility and provides security to our data. But when our application access data from the database and stores in the variable, Now It can be theft.
Data should not travel openly. Must have a container to wrap that same as In the real world(from this point you should know that how a real-world object will be defined in a program),
If I am an object of the Human class, with variables
- name: "foo"
- height: 5.8
- color: "fair"
Nobody can know my name by just looking at me without asking. So now name became secured. To wrapping up data like this in a class is called Encapsulation.
In code:
class Human{
var name = "foo";
var height = 5.8;
var color = "fair"
public static void main(String[] args){
Human object1 = new Human();
}
public static void main(String[] args){
Human object1 = new Human();
}
}
In the code you must do like object1.name then you'll get the name.Even data became secured from other objects also. For an instance, If I will workout, I'll get the Muscular body, not my neighbor. In the same way, If one object changed it's variable's value, the variable will not be changed for any other objects except this. i.e variables and functions of one object cannot be accessed by other objects, and it just because of Encapsulation.
for example(in code):
class Human{
var name;
var height;
var color;
var weight;
public Human(String name, float height, String color, float weight){
name = name;
height = height;
color = color;
weight = weight;
}
public void workout(){
weight = weight + 5;
}
public static void main(String[] args){
Human object1 = new Human("foo", 5.7, "fair", 70);
Human object2 = new Human("bar", 5.9, "fair", 70);
object1.workout();
// object1 did workout, object2 not
System.out.println(object1.weight) // 75
System.out.println(object2.weight) // 70
}
}
Comments
Post a Comment