Chapter 4 - Using Classes & Objects

Overview of Java Classes

  • Java Classes can be differentiated into the following types:

    • Non-instantiable classes: Classes from which objects are not instantiated.

    • Instantiable classes: Classes from which you can create objects.

Object-Oriented Programming Concepts
  • In object-oriented programming:

    • Everything is an object.

    • Every object is a member of a class.

Learning About Classes and Objects

  • Is-a relationship:

    • Describes a relationship wherein the object is a concrete example of the class.

    • An object is an instantiation of a class.

Examples of Is-a Relationship
  • Example:

    • Desk: Your desk is an instance of the Desk class.

    • Correct expression: "My oak desk with the scratch on top is a Desk."

    • Incorrect expression: "A Desk is an oak desk with a scratch on top."

Classes and Objects
  • Classes provide reusability, while objects derive their attributes from their classes.

  • Class Client/User:

    • An application or class that instantiates objects of another class.

Analogy:

  • The relationship between classes and objects is like a recipe for apple pie and the actual apple pies made from the recipe.

Attributes and Methods of Classes

Data Fields
  • Data Fields:

    • Represent the attributes of an object by storing current values.

    • For example:

    • A circle object has a data field radius.

    • A rectangle object has data fields width and height.

  • Data fields are variables declared within a class but outside any method.

Methods
  • Methods define the behavior (actions) of an object.

  • Invoking a Method:

    • Asking an object to perform an action.

    • Examples:

    • getArea(): Retrieves the area of a circle object.

    • getPerimeter(): Retrieves the perimeter of a circle object.

    • setRadius(radius): Changes the radius of a circle object.

Class Definition Example
  • Circle Class:

    • Data Fields: radius

    • Methods: getArea(), getPerimeter(), setRadius()

  • Example of Class Objects:

    • Object with radius 1, Object with radius 25, and Object with radius 125.

Creating Classes

Class Header
  • Composed of:

    • Optional access specifier (e.g., public)

    • The keyword class

    • A legal identifier (class name)

  • Example of a class header:

    • public class Employee allows access by all objects and can be extended.

Instance Variables and Access Modifiers
  • Instance Variables:

    • There is one copy per object instantiation.

    • Non-static data fields belong to the instance of the class.

  • Private Access:

    • No access from other classes; restricted to the class’s own methods.

    • Information Hiding: Private data can only be changed by the class’s methods.

Example of a Circle Class Definition
public class Circle {
  private double radius;

  public double getArea() {
    return radius * radius * 3.1415;
  }

  public double getPerimeter() {
    return 2 * radius * 3.1415;
  }

  public void setRadius(double newRadius) {
    radius = newRadius;
  }
}

Creating Instance Methods in a Class

  • Methods that modify field values: Mutator Methods (prefix set).

  • Methods that retrieve values: Accessor Methods (prefix get).

Understanding Static vs. Non-Static

Static Fields and Methods
  • Static: Belongs to the class, rather than instances.

  • You can use static fields without instantiating an object.

  • Example: For 100 instances of a class with a static field, there’s one copy in memory.

Non-Static Fields and Methods
  • Default behavior unless explicitly declared static.

  • For 100 instances, each has its copy.

  • Example: getEmpNum() must be non-static to return a unique value for each Employee object.

Declaring Objects and Using Their Methods

  • Declaring an Object does not create an actual instance.

  • Two-Step Process:

    1. Supply type and identifier.

    2. Use new to allocate memory for the object.

Example of Object Declaration and Allocation
Employee someEmployee;
someEmployee = new Employee(); // Two statements
// Or, in one statement:
Employee someEmployee = new Employee();

Class Assignment Example

  • Task to create an Orders class:

    1. Instance variables for customer details and cost.

    2. Create getters/setters for instance variables.

    3. Implement a method that interacts with the user.

Working with Constructors
Default Constructor
  • Automatically provided when no other constructor is defined.

  • Initializes basic values:

    • Numeric fields to 0

    • Boolean fields to false

    • Reference fields to null

Using Constructors with Parameters
  • Allows initialization of object fields upon creation.

  • Example:

public class Employee {
  private int empNum;
  public Employee(int num) {
    empNum = num;
  }
}
  • When creating an object, provide the necessary argument:

Employee aWorker = new Employee(7677);
Using Constructors Effectively
  • Use the this reference to differentiate between instance variables and method parameters.

Viewing and Understanding the this Reference
  • this is reserved word in Java that refers to the current instance of the class.

  • Automatically passed to any non-static method within a class.

  • Helps avoid ambiguity between instance and parameter names.