Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Encapsulation in Java

 
Greenhorn
Posts: 4
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anybody tell is JAVABEANS a proper example of Encaptulation?? And how encaptulation is handle in java, what is the use of encaptulation ? Why it came in JAVA?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

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.
 
Ranch Hand
Posts: 171
Hibernate Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Prashant, Welcome to coderanch. did you try googling it? i am sure you'll find a lot of useful results. one such result is here http://www.tutorialspoint.com/java/java_encapsulation.htm
 
Ranch Hand
Posts: 175
17
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic