Java
  Home arrow Java arrow Page 3 - Java Part 4: Objects and Information Hidin...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Java Part 4: Objects and Information Hiding
By: Chris Noack
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2001-12-12

    Table of Contents:
  • Java Part 4: Objects and Information Hiding
  • Objects and "this"
  • Enforcing information hiding
  • The private and public declarations
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Java Part 4: Objects and Information Hiding - Enforcing information hiding


    (Page 3 of 5 )

    Information hiding is a method that good programmers use to make their code more reusable. It is best to make the variables in a class invisible and use a set of visible methods to access these variables. This means that the user will not have to know how the internals of the class work, rather just how its member functions should be called.

    We make member variables, such as “value” invisible so that they can’t be accessed directly by other parts of the application. Only members of the same class can access them internally. The getVal() member function was implemented to enforce this.

    We also create a class like this so that the user cannot directly reference a member variable, for instance:

    number.value += 3;

    This is a bad design habit, because the "value" variable is referenced and changed directly. A more correct approach would be:

    number.addTo( 3 );

    This approach is more sensible, and doesn’t allow the user to directly modify the “value” variable.

    Java provides formal support for information hiding. In C, there is no formal support for information hiding, and the programmer must implement setter and accessor functions to manipulate data involved with a abstract data type (an abstract data type is a collection of data, as well as the functions that operate on that data). A letdown with the C language, is that the user can circumvent these functions and access the data directly.

    More Java Articles
    More By Chris Noack


     

    JAVA ARTICLES

    - Deploying Multiple Java Applets as One
    - Deploying Java Applets
    - Understanding Deployment Frameworks
    - Database Programming in Java Using JDBC
    - Extension Interfaces and SAX
    - Entities, Handlers and SAX
    - Advanced SAX
    - Conversions and Java Print Streams
    - Formatters and Java Print Streams
    - Java Print Streams
    - Wildcards, Arrays, and Generics in Java
    - Wildcards and Generic Methods in Java
    - Finishing the Project: Java Web Development ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT