<<< Class interface     Index     Inheritance >>>

3. Class implementation


  • Parts of the class not included in its public interface belong to implementation:

    • private attrbutes

    • private Java methods (private C++ member functions.)

  • Compiler prevents users from gaining access to the class implementation.

  • Implementation is hidden from the user.

  • Java example of the interface/implementation:

    
    public class IntSquare {
        // private attribute
        private int squareValue;
    
        // public interface
        public int getSquare( int value ) {
            SquareValue = calculateSquare( value );
            return squareValue;
        }
    
        // private implementation
        private int calculateSquare( int value ) {
            return value * value;
        }
    }
    
    
  • Corresponding UML diagram:

     

    IntSquare
    -squareValue:int
    +getSquare:int
    -calculateSquare:int

  • If implementation changes, the interface remains the same.

<<< Class interface     Index     Inheritance >>>