Encapsulation (<= click the link) is a principle in object oriented programming. It's a bit hard to explain in one or two sentences. It has to do with other OO programming principles. One of them is that you want parts of your program to have low coupling - which means that, for example, two different classes should not need to know more about each other than really necessary. This is because when you would have high coupling (two classes that know and rely on all the details of each other's implementations), the source code of your program will become a complicated mess. By letting the classes not rely on each other more than necessary, you can keep your code simpler and easier to understand.
Encapsulation is a way to hide parts of classes so that other classes cannot directly access them, and this helps you with low coupling.
Please use a better title for your posts than just "Java". The forums are easier to use if you use a title that explains what your post is about.
Prashant kr Sinha wrote:Can anybody tell is JAVABEANS a proper example of Encaptulation??
They both can be used to achieve reusability but in different ways.
With encapsulation, your objective is to ensure that certain internal implementation details of a class are not exposed to collaborating classes so that you can change these internal implementation details without changing collaborating classes. Because a class is loosely coupled to collaborating classes, it is more reusable i.e. a class in one application can be easily reused in a different application.
With JavaBeans, your objective is to ensure that your classes can be used by certain tools, for example, code builder tools. When your classes follow the JavaBeans specification (serializability, access to properties via getters and setters, no-arg constructor), code builder tools can easily inspect and modify your objects. You can use a code builder tool to quickly build applications by dragging and dropping simple Java objects i.e. simple Java objects can be easily composed into more complex components. A component in one application can be easily reused by a code builder tool to build a different application.
These days, many frameworks (like Spring) use POJOs because they are simpler than JavaBeans. Unlike a JavaBean which must conform to certain restrictions, a POJO (plain old java object) does not conform to any restrictions other than those imposed by Java.
Of course, I found a very beautiful couch. Definitely. And this tiny ad: