Home arrow Java arrow Page 3 - Java Part 4: Objects and Information Hiding

Java Part 4: Objects and Information Hiding

In part three of this ten part tutorial, Chris discussed some of the primitive data types available in Java. He also went through a basic Java class. In this article, he will explore that class further and discuss some aspects of objects, as well as information hiding.

Author Info:
By: Chris Noack
Rating: 3 stars3 stars3 stars3 stars3 stars / 6
December 12, 2001
  1. · Java Part 4: Objects and Information Hiding
  2. · Objects and "this"
  3. · Enforcing information hiding
  4. · The private and public declarations
  5. · Conclusion

print this article

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.
blog comments powered by Disqus

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- 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 ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials