|
|
| java |
Expert:bhavna
pl. let me know few progrograms on encapsulation. thanks
|
| Answers |
Hi Friend,
class Cuboid { private int length; private int width; private int height; public void setLength(int l) {length = l;} public void setWidth(int b) {width = b;} public void setHeight(int h) {height = h;} public int displayVolume() {return (length*width*height);} } class Encapsulation { public static void main (String args[]) { Cuboid c = new Cuboid(); double vol; c.setLength(20); c.setWidth(10); c.setHeight(5); vol = c.displayVolume(); System.out.println("Volume is : " + vol); } }
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.Any class that wants to access the variables should access them through these getters and setters.
Thanks
|
| More Questions |
|
|
Post Answers
Ask Question
Facing Programming Problem?
|
|
|
|
|