Chapter 7 Objects and Classes

 

Objectives

·        To understand objects and classes and use classes to model objects (§7.2).

·        To learn how to declare a class and how to create an object of a class (§7.3).

·        To understand the roles of constructors and use constructors to create objects (§7.3).

·        To use UML graphical notations to describe classes and objects (§7.3).

·        To distinguish between object reference variables and primitive data type variables (§7.4).

·        To use classes in the Java library (§7.5).

·        To declare private data fields with appropriate get and set methods to make class easy to maintain (§7.6-7.8).

·        To develop methods with object arguments (§7.9).

·        To understand the difference between instance and static variables and methods (§7.10).

·        To determine the scope of variables in the context of a class (§7.11).

·        To use the keyword this as the reference to the current object that invokes the instance method (§7.12).

·        To store and process objects in arrays (§7.13).

·        To apply class abstraction to develop software (§7.14).

·        To declare inner classes (§7.17 Optional).

 

 

 

OO Programming Concepts

 

Object-oriented programming (OOP) involves programming using objects. An object represents an entity in the real world that can be distinctly identified. For example, a student, a desk, a circle, a button, and even a loan can all be viewed as objects. An object has a unique identity, state, and behaviors. The state of an object consists of a set of data fields (also known as properties) with their current values. The behavior of an object is defined by a set of methods.

 

Objects

 

 

 

 

 

 

 

 

 

 


Classes

 

An object has both a state and behavior. The state defines the object, and the behavior defines what the object does.

 

Classes are constructs that define objects of the same type. A Java class uses variables to define data fields and methods to define behaviors. Additionally, a class provides a special type of methods, known as constructors, which are invoked to construct objects from the class.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


UML Class Diagram

 

 

 

 

 

 

 

 

 

 

 

 

 


Constructors

 

 

Constructors are a special kind of methods that are invoked to construct objects.

 

 

Circle() {

}

 

Circle(double newRadius) { 

  radius = newRadius;

}

.

Creating Objects Using Constructors

new ClassName();

 

Example:

new Circle();

 

new Circle(5.0);

 

 

A constructor with no parameters is referred to as a no-arg constructor.

·       Constructors must have the same name as the class itself.

·       Constructors do not have a return type—not even void.

·       Constructors are invoked using the new operator when an object is created. Constructors play the role of initializing objects.

 

 

Declaring Object Reference Variables

 

 

To reference an object, assign the object to a reference variable.

 

To declare a reference variable, use the syntax:

 

ClassName objectRefVar;

 

Example:

Circle myCircle;

 

 

Declaring/Creating Objects
in a Single Step

ClassName objectRefVar = new ClassName();

 

Example:

Circle myCircle = new Circle();

 

Accessing Objects

 

·        Referencing the object’s data:

·        objectRefVar.data

·        e.g., myCircle.radius

 

·        Invoking the object’s method:

·        objectRefVar.methodName(arguments)

·        e.g., myCircle.getArea()

 

 

Caution

Recall that you use

Math.methodName(arguments) (e.g., Math.pow(3, 2.5))

to invoke a method in the Math class. Can you invoke getArea() using Circle1.getArea()? The answer is no. All the methods used before this chapter are static methods, which are defined using the static keyword. However, getArea() is non-static. It must be invoked from an object using

 

objectRefVar.methodName(arguments) (e.g., myCircle.getArea()).

 

More explanations will be given in Section “Static Variables, Constants, and Methods.”

 

 

Reference Data Fields

The data fields can be of reference types. For example, the following Student class contains a data field name of the String type.

 

public class Student {

  String name; // name has default value null

  int age; // age has default value 0

  boolean isScienceMajor; // isScienceMajor has default value false

  char gender; // c has default value '\u0000'

 

 

The null Value

If a data field of a reference type does not reference any object, the data field holds a special literal value, null.

 

Default Value for a Data Field

The default value of a data field is null for a reference type, 0 for a numeric type, false for a boolean type, and '\u0000' for a char type. However, Java assigns no default value to a local variable inside a method.

Example

public class Test {

  public static void main(String[] args) {

    int x; // x has no default value

    String y; // y has no default value

    System.out.println("x is " + x);

    System.out.println("y is " + y);

  }

}

Differences between Variables of
Primitive Data Types and Object Types

 

 

 

 

 

 

 




Copying Variables of Primitive Data Types and Object Types

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Garbage Collection

As shown in the previous figure, after the assignment statement c1 = c2, c1 points to the same object referenced by c2. The object previously referenced by c1 is no longer referenced. This object is known as garbage.

 

 

Instance
 Variables, and Methods

Instance variables belong to a specific instance.

Instance methods are invoked by an instance of the class.

 

Static Variables, Constants,
and Methods

 

Static variables are shared by all the instances of the class.
Static methods are not tied to a specific object.

Static constants are final variables shared by all the instances of the class.

To declare static variables, constants, and methods, use the static modifier.

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Example of
Using Instance and Class Variables and Method

   Objective: Demonstrate the roles of instance and class variables and their uses. This example adds a class variable numberOfObjects to track the number of Circle objects created.

 

public class Circle2 {
  /** The radius of the circle */
  double radius;
 
  /** The number of the objects created */
  static int numberOfObjects = 0;
 
  /** Construct a circle with radius 1 */
  Circle2() {
    radius = 1.0;
    numberOfObjects++;
  }
 
  /** Construct a circle with a specified radius */
  Circle2(double newRadius) {
    radius = newRadius;
    numberOfObjects++;
  }
 
  /** Return numberOfObjects */
  static int getNumberOfObjects() {
    return numberOfObjects;
  }
 
  /** Return the area of this circle */
  double getArea() {
    return radius * radius * Math.PI;
  }
}

 

 

public class TestCircle2 {
  /** Main method */
  public static void main(String[] args) {
    // Create c1
    Circle2 c1 = new Circle2();
 
    // Display c1 BEFORE c2 is created
    System.out.println("Before creating c2");
    System.out.println("c1 is : radius (" + c1.radius +
      ") and number of Circle objects (" +
      c1.numberOfObjects + ")");
 
    // Create c2
    Circle2 c2 = new Circle2(5);
 
    // Change the radius in c1
    c1.radius = 9;
 
    // Display c1 and c2 AFTER c2 was created
    System.out.println("\nAfter creating c2 and modifying " +
      "c1's radius to 9");
    System.out.println("c1 is : radius (" + c1.radius +
      ") and number of Circle objects (" +
      c1.numberOfObjects + ")");
    System.out.println("c2 is : radius (" + c2.radius +
      ") and number of Circle objects (" +
      c2.numberOfObjects + ")");
  }
}

 

 

Visibility Modifiers and
Accessor/Mutator Methods

 

By default, the class, variable, or method can be
accessed by any class in the same package.

 

·        Public:  The class, data, or method is visible to any class in any package.

·        Private:  The data or methods can be accessed only by the declaring class.

 

The get and set methods are used to read and modify private properties.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


The private modifier restricts access to within a class, the default modifier restricts access to within a package, and the public modifier enables unrestricted access.

 

 

NOTE

Why Data Fields Should Be private?

To protect data.

To make class easy to maintain.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Why Data Fields Should Be Private?

 

To protect data.

To make class easy to maintain.

Example of Data Field Encapsulation

 

 

 

 

 

 

 

 

 

 

 

 

 


public class Circle3 {
  /** The radius of the circle */
  private double radius = 1;
 
  /** The number of the objects created */
  private static int numberOfObjects = 0;
 
  /** Construct a circle with radius 1 */
  public Circle3() {
    numberOfObjects++;
  }
 
  /** Construct a circle with a specified radius */
  public Circle3(double newRadius) {
    radius = newRadius;
    numberOfObjects++;
  }
 
  /** Return radius */
  public double getRadius() {
    return radius;
  }
 
  /** Set a new radius */
  public void setRadius(double newRadius) {
    radius = (newRadius >= 0) ? newRadius : 0;
  }
 
  /** Return numberOfObjects */
  public static int getNumberOfObjects() {
    return numberOfObjects;
  }
 
  /** Return the area of this circle */
  public double getArea() {
    return radius * radius * Math.PI;
  }
}

 

// TestCircle3.java: Demonstrate private modifier
public class TestCircle3 {
  /** Main method */
  public static void main(String[] args) {
    // Create a Circle with radius 5.0
    Circle myCircle = new Circle(5.0);
    System.out.println("The area of the circle of radius "
      + myCircle.getRadius() + " is " + myCircle.getArea());
 
    // Increase myCircle's radius by 10%
    myCircle.setRadius(myCircle.getRadius() * 1.1);
    System.out.println("The area of the circle of radius "
      + myCircle.getRadius() + " is " + myCircle.getArea());
  }
}

 

Immutable Objects and Classes

 

If the contents of an object cannot be changed once the object is created, the object is called an immutable object and its class is called an immutable class. If you delete the set method in the Circle class in the preceding example, the class would be immutable because radius is private and cannot be changed without a set method.

 

Example

 

public class Student {
  private int id;
  private BirthDate birthDate;

  public Student(int ssn,
      int year, int month, int day) {
    id = ssn;
    birthDate = new BirthDate(year, month, day);
  }

  public int getId() {
    return id;
  }

  public BirthDate getBirthDate() {
    return birthDate;
  }
}

 

public class BirthDate {
  private int year;
  private int month;
  private int day;
 
  public BirthDate(int newYear,
      int newMonth, int newDay) {
    year = newYear;
    month = newMonth;
    day = newDay;
  }
 
  public void setYear(int newYear) {
    year = newYear;
  }
}

 

public class Test {
  public static void main(String[] args) {
    Student student = new Student(111223333, 1970, 5, 3);
    BirthDate date = student.getBirthDate();
    date.setYear(2010); // Now the student birth year is changed!
  }
}

 

What Class is Immutable?

 

For a class to be immutable, it must mark all data fields private and provide no mutator methods and no accessor methods that would return a reference to a mutable data field object.

 

Passing Objects to Methods

·        Passing by value for primitive type value (the value is passed to the parameter)

·        Passing by value for reference type value (the value is the reference to the object)

 

 

Scope

·        The scope of instance and static variables is the entire class. They can be declared anywhere inside a class.

·        The scope of a local variable starts from its declaration and continues to the end of the block that contains the variable. A local variable must be initialized explicitly before it can be used.

 

 

The this Keyword

·        Use this to refer to the object that invokes the instance method.

·        Use this to refer to an instance data field.

·        Use this to invoke an overloaded constructor of the same class.

 

 

 

 

Array of Objects

 Circle[] circleArray = new Circle[10];

 

 An array of objects is actually an array of reference variables. So invoking circleArray[1].getArea() involves two levels of referencing as shown in the next figure. circleArray references to the entire array. circleArray[1] references to a Circle object.

 

   Circle[] circleArray = new Circle[10];

 

 

 

 

 

 

 

 

 

 


public class TotalArea {
  /** Main method */
  public static void main(String[] args) {
    // Declare circleArray
    Circle3[] circleArray;
 
    // Create circleArray
    circleArray = createCircleArray();
 
    // Print circleArray and total areas of the circles
    printCircleArray(circleArray);
  }
 
  /** Create an array of Circle objects */
  public static Circle3[] createCircleArray() {
    Circle3[] circleArray = new Circle3[10];
 
    for (int i = 0; i < circleArray.length; i++) {
      circleArray[i] = new Circle3(Math.random() * 100);
    }
 
    // Return Circle array
    return circleArray;
  }
 
  /** Print an array of circles and their total area */
  public static void printCircleArray
      (Circle3[] circleArray) {
    System.out.println("Radius\t\t\t\t" + "Area");
    for (int i = 0; i < circleArray.length; i++) {
      System.out.print(circleArray[i].getRadius() + "\t\t" +
        circleArray[i].getArea() + '\n');
    }
 
    System.out.println("-----------------------------------------");
 
    // Compute and display the result
    System.out.println("The total areas of circles is \t" +
      sum(circleArray));
  }
 
  /** Add circle areas */
  public static double sum(Circle3[] circleArray) {
    // Initialize sum
    double sum = 0;
 
    // Add areas to sum
    for (int i = 0; i < circleArray.length; i++)
      sum += circleArray[i].getArea();
 
    return sum;
  }
}