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

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
TABLE OF CONTENTS:
  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
SEARCH DEVARTICLES

Java Part 4: Objects and Information Hiding - The private and public declarations
(Page 4 of 5 )

Where we defined the single float variable, “value” of the Number class, we could declare it as a private member variable. This would prevent the “value” variable from being able to be modified from outside of the class, directly. The “value” variable can be seen within the class, however we will write some accessor and setter methods to manipulate the variable indirectly. This is good practice, as mentioned above:

private float value;

This is the only change necessary to implement the information hiding for the “value” variable. Everything member function should be made public, so that they can be seen (and accessed) from outside of the class. If we were to make the member functions private, then any call to that function would raise an error, because it would be invisible outside of the class.

Let’s rewrite the Number class to enforce information hiding. The complete Number class is shown below, for your convenience:

// The class definition

public class Number{

// A Class member float type, set to zero by default.

private float value = 0;

// The default constructor

public Number(){}

// Another Constructor

Number( float val ){

value = val;

}

public static void main( String[] args ){

Number number = new Number();

number.addTo( 5 );

number.divideBy( 5 );

System.out.println( +number.getVal() );

System.exit(1);

}

// The Following 4 methods manipulate

public float addTo( float x ){

// the class member "value"

return value += x;

}

public float subFrom( float x ){

return value -= x;

}

public float multiplyBy( float x ){

return value *= x;

}

public float divideBy( float x ){

if( value == 0 ){

return 0;

}

return value /= x;

}

// An accessor method, simply returns the value of the "value" member

public float getVal(){

return value;

}

}


There we have it. The Number class now adheres to the information hiding conventions. The Number class will now be more re-usable because the

implementation of the class may be changed without the user having to re-learn how to use it (that is, if the methods remain unchanged).

The user can still violate the rules within the class. For instance, inside the main function, the “value” variable can still be directly referenced. Anywhere outside of the class, however, this will produce an error.
blog comments powered by Disqus
JAVA ARTICLES

- 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 
Support 

Developer Shed Affiliates

 




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