View RSS Feed

Java Basic

Encapsulation

Rate this Entry
by , 01-08-2012 at 11:14 AM (969 Views)
To store all the variables and methods in an object is called encapsulation. Variables are declared private to hide them and getter methods are provided to access those variables. It is concept of hiding internal functionality. It helps in improving code modularity and re usability which supports refactoring. It is also important for integrity of application to encapsulate it internal variables and methods. Below code explains process of encapsulation in depth where different objects are kept hidden from outer world.

Java Code: This is an example of encapsulation
Class MyMarks {
private int vmarks = 0;
private String name;
public void setMarks(int mark)
throws MarkException {
if(mark > 0)
this.vmarks = mark;
else {
throw new MarkException("No negative
Values");
}
}
public int getMarks(){
return vmarks;
}
//getters and setters for attribute name goes here.
}

Submit "Encapsulation" to Facebook Submit "Encapsulation" to Digg Submit "Encapsulation" to del.icio.us Submit "Encapsulation" to StumbleUpon Submit "Encapsulation" to Google

Updated 01-14-2012 at 06:39 PM by Java Basic

Categories
Tutorial

Comments