Encapsulation
by , 01-08-2012 at 11:14 AM (660 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 encapsulationClass 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. }









Email Blog Entry
Size Reduced for Images in PDF &...
05-15-2013, 05:53 PM in Java Software