[DATA STRUCTURE & Other Objects Using JAVA] Chapter 2 Java Classes and Information Hiding

2.0 Introduction

  • Object-oriented programming is an approach to programming in which data occurs in tidy packages called objects.
  • The separation of specification from implementation is an example of information hiding.
  • ADT (abstract data type): In computer science, an abstract data type (ADT) is a mathematical model for a certain class of data structures that have similar behavior; or for certain data types of one or more programming languages that have similar semantics. (from wikipedia)

2.1 Classes and Their Members

  • Member: Taken together, the data (instance variables), constructors, and methods of a class are called the class members.
  • class head: consists of the Java key word public class followed by the name of the new class.

  • Always use a capital letter for the first character of names of new classes.

  • public & private

2.1.1 Constructors

  • Before any constructor begins its work, all instance variable are assigned Java “default values” which is zero.
  • The name of a con structure must be the same as the name of the class.
  • A constructor is not really a method, and therefore it does not have any return value.
  • Some classes may have more than one constructor, and those constructors are distinguished by distinct sequence of parameters.

2.1.2 No-Arguments Constructors

  • no-argument constructor: a constructor with no argument
  • If you write a class with no constructs at all, then Java automatically provides a non-arguments construct that initializes each instance variable to its initialization value or its default value.

2.1.3 Methods

  • Accessor method: It gives information about an object without altering the object.
  • Modification method: It can change the status of an object.
  • Accessor method often have no parameters, no precondition, and only a simple return condition in the specification.

Why private and accessor method?

  1. Don’t need to know how the class is implemented (don’t need to know what instance variable the class has, only need to know public functions)
  2. Programs use the class will still work when the implementation of class is changed, because the accessor methods are not changed.
  3. It is easy to test the accessor method, which makes the class more reliable.
  4. The pattern of “private data, public method” is able to forbid other programmers from using instance variables in unintended ways (such as setting numberOfPeople to negative).
  • Modification methods are usually void, meaning there is no return value. Most modification methods have parameters.
  • Arithmetic overflow: trying to compute or store a number that is beyond the legal range of the data type.
  • The name of the Java file must match the name of the class.

2.2 Using Class

2.2.1 Creating and Using Objects

  • Call a method = activate a method
  • When a program has several objects of the same class, each object has its own copies of the class’s instance variable.
  • Reference variable: used to refer to objects

2.2.2 Null Reference

  • Null reference: Sometimes a reference variable does not refer to anything. This is a null reference, and the value of the variable is called null.

NULLPOINTEREXCEPTION

If a variable is null, then it is a error to activate a method of it. However, it is fine to send message to NULL in Objective-C, which does nothing.

2.2.3 Assignment Statements with Reference Variables

  • If t1 and t2 are reference variables, then the assignment t2 = t1 is allowed.
  • If t1 is null, then the assignment makes t2 null also.
  • It t1 is not null, then the assignment changes t2 so that it refers to the same object that t1 already refers to. At this point, changes can be made to that one object through either t1 or t2.

2.2.4 Clone

  • Clone: A separate copy is a copy of the original object which should not alter the original, nor should subsequent changes to the original alter the copy. (Deep copy)

2.2.5 Test for Equality with Reference Variables

For reference variable t1 and t2, the test (t1 == t2) is true if both references are null or if t1 and t2 refer to the exact same object.

2.3 Packages

2.3.1 Declaring a Package

  • Using the Internet domain name in reserve as a prefix to name package

  • Package declaration

  • Place the package at the right place

    1. Suppose the pwd is a folder called classes.
    2. Underneath the classes directory, crate a subdirectory called com.
    3. Underneath comm, create a subdirectory called brucedsu.
    4. Underneath bruecedsu, create a subdirectory called PACKAGE_NAME
    5. All the .java and .class files for the package mush be placed in the PACKAGE_NAME subdirectory.

2.3.2 The Import Statement to Use a Package

‘*’ means import all classes.

Only import the class called ‘CLASS_NAME’.

2.3.3 The JCL Packages

  • JCL: Java Class Libraries
  • Package java.lang is so useful that is automatically imported into every Java program.

2.3.4 More about Public, Private, and Package Access

  • Access modifiers: The keywords public and private are called the access modifiers because they control access to the class members.

2.4 Parameters, Equals Method, and Clones

2.4.1 Static Method

  • The static keyword means that the method is not activated by any one object.

2.4.2 Parameters That Are Objects

  • When a parameter is an object, then the parameter is initialized so that it refers to the same object that the actual argument refers to.
  • Double.Nan: a constant that a program uses to indicate that a double value is “not a number”.
  • Double.POSITIVE_INFINITY
  • Double.NEGATIVE_INFINITY

2.4.3 Methods May Access Private Instance Variables of Objects in Their Own Class

A method may access private instance variables of an object as long as the method is declared as part of the same class as the object.

2.4.4 How to Choose the Name of Method?

Accessor Methods

  • The name of a boolean accessor method will usually begin with “is” followed by an adjective, such as isOn().
  • Methods that convert to another kind of data start with “to”, such as toString().
  • Other accessor method start with ‘get’ or some other verb followed by a noun that describes the return value, such as getFLow().

Modification Methods

A modification method can be named by a descriptive verb, such as shift(), or a short verb phrase, such as shutOff().

Static Methods that Returns a Value

Try to use a noun that describes the return object, such as distance() or midpoint().

2.4.5 Java’s Object Type

In Java, Object is a kind of “super data type” that encompasses all data except the eight primitive types (byte, short, int, long, float, double, boolean, char). In Objective-C, NSObject is the “super data type”.

2.4.6 Using and Implementing an equals() Method

  • When p itself is null, it is a programming error to activate any method of p. Trying to activate any method when p is null results in a NullPointerException.
  • instanceof: is an boolean operator. On the right of the operator is a class name, such as Location. The test returns true if it is valid to convert the object to the given data type.
  • To avid ClassCastException use instanceof first. In Objective-C, use isKindOfClass(__unsafe_unretained Class).

2.4.7 Implementing a clone() Method

1. Modify the Class Head

Add the words “implements Cloneable” in the class head. Just like conforms to protocol in Objective-C.

2. Use super.clone() to Make a Copy

super.clone() is actually clone() method from Java’s Object type.

This exception is thrown by the clone() method from Java’s Objet type when a programmer tries to call super.clone() without including the implements Cloneable clause as part of the class definition.

3. Make Necessary Modification and Return

2.4.8 Java Parameters

  • The eight primitive types: The parameters is initialized with the value of the argument. Subsequent changes to the parameter do not effect the argument.
  • Reference variables: When a parameter is a reference variable, the parameter is initialized so that it refers to the same object as the actual argument. Subsequent changes to this object do affect the actual argument’s object.