Encapsulation is one of the four fundamental features in object oriented programming. The others are inheritance, polymorphism, and abstraction.
Encapsulation is the mechanism of binding together code and the data members on which it (code) works thus preventing them from unauthorized access. Encapsulation can be described as a protective wrapper that prevents the code and data from being accessed by other code defined outside the class. Access to the data and code inside the wrapper is tightly controlled by a well defined interface. To relate this to the real world, we can consider our TV set. It encapsulates number of physical components and functions inside it. We can communicate with the components using the functions defined in the set. Thus we can operate the TV set without bothering about the complexity of the TV set architecture. The same idea is applicable to programming also. We can use the encapsulated codes to get our job done without knowing details about it.
In Java the basis of encapsulation is the class. A class defines the structure and behavior – data and code that will be shared by a set of objects. Each object of a given class contains the structure and behavior defined by the class. For this reason objects are known as instance of a class.
Purpose of a class is to encapsulate complexity.
Let us look at an example
public class Test
{
private String name;
private int age;
public int getAge()
{
return age;
}
public String getName()
{
return name;
}
public void setAge( int newAge)
{
age = newAge;
}
public void setName(String newName)
{
name = newName;
}
}
The public functions can be accessed from outside the class but the private data members can not be accessed by codes which reside outside the class. Therefore any class that wants to access the variables should access them through these functions defined in the class without knowing details of the statements written inside the function body. This is encapsulation.
Any doubt? Put Comments.
Encapsulation is the mechanism of binding together code and the data members on which it (code) works thus preventing them from unauthorized access. Encapsulation can be described as a protective wrapper that prevents the code and data from being accessed by other code defined outside the class. Access to the data and code inside the wrapper is tightly controlled by a well defined interface. To relate this to the real world, we can consider our TV set. It encapsulates number of physical components and functions inside it. We can communicate with the components using the functions defined in the set. Thus we can operate the TV set without bothering about the complexity of the TV set architecture. The same idea is applicable to programming also. We can use the encapsulated codes to get our job done without knowing details about it.
In Java the basis of encapsulation is the class. A class defines the structure and behavior – data and code that will be shared by a set of objects. Each object of a given class contains the structure and behavior defined by the class. For this reason objects are known as instance of a class.
Purpose of a class is to encapsulate complexity.
Let us look at an example
public class Test
{
private String name;
private int age;
public int getAge()
{
return age;
}
public String getName()
{
return name;
}
public void setAge( int newAge)
{
age = newAge;
}
public void setName(String newName)
{
name = newName;
}
}
The public functions can be accessed from outside the class but the private data members can not be accessed by codes which reside outside the class. Therefore any class that wants to access the variables should access them through these functions defined in the class without knowing details of the statements written inside the function body. This is encapsulation.
Any doubt? Put Comments.
could you make it into a full coding so that i cant run it..
ReplyDelete:D thanks!
Actually in BlueJ editor we can execute the program without main() method. In other editors , simply define the main() function within the class.
ReplyDeletepublic class Test{
private String name;
private int age;
public int getAge(){
return age;
}
public String getName(){
return name;
}
public void setAge( int newAge){
age = newAge;
}
public void setName(String newName){
name = newName;
}
public static void main(String args[])
{
String myName;
int myAge;
Test ob=new Test();
ob.setAge(20);
ob.setName("Suddhashil");
myAge=ob.setAge();
myName=ob.myName();
System.out.println("My Name="+ myName + " And My Age="+ myAge);
}
}