Lecture 4: Object-Based Programming

What You Will Learn Today

  1. Define and give examples of objects, classes, fields and methods.
  2. Define and give examples of class libraries, APIs and packages.
  3. Explain the use of the import declaration.
  4. Declare, define and use fields and methods.
  5. Differentiate field and method access control qualifiers (public, private, protected).
  6. Create class fields and methods using the static modifier.
  7. Put the features of a class definition in a consistent sequence.
  8. List steps to design a class.

Objects, Classes, Fields and Methods

object
representation of a specific person, thing or concept; usually named with a noun in English
an instance of a class; has attributes and methods; first letter is small
e.g. a specific tree, dog, person, student, car, ball, coin, book, bankAccount, string, array, rectangle, matrix, errorMessage
class
a blueprint, template, model, pattern or factory to produce objects with similar features, usually a singular noun, first letter is capitalized
e.g. all tree objects belong to the class Tree; all people belong to the class Person
field or attribute
a variable that represents the object's state, that stores information about an object (properties and characteristics)
e.g. a person's name, birthdate, address and phone number; a ball's position, diameter and colour
method
a function that represents an object's behaviour; usually named with a verb in English
(something the object can do, or something that can be done to or with the object)
e.g. throw, bounce or roll a ball; deposit or withdraw money from a bank account; calculate a person's age

Examples of Classes and Objects

Class Libraries, APIs and Packages

class library
a set of reusable classes which supports development of programs
e.g. the Java standard class library
Application Programmer Interface (API)
group of related classes
e.g. the Java database API for databases, Swing for graphical user interfaces
package
group of related classes with a structured name
e.g. java.lang (language support); java.io (input/output); java.util (utilities); java.awt (graphics and GUI);
javax.swing (GUI); java.applet (applets); java.net (networking); java.sql (database access)

Import Declaration

Declaring and Using Fields

Defining Methods

Calling Methods

Constructor Methods

Access Control for Fields

Access Control for Methods

Class Fields

Class Methods

Recommended Class Structure

  1. class comment explaining the purpose of the class
  2. class name and qualifiers
  3. class body: put all public features before all private features; each should include:
    1. methods (separated by a blank line)
      1. constructors
      2. instance methods
      3. static methods
    2. fields
      1. instance fields
      2. static fields
    3. inner classes (hidden classes only declared and used within a class)

Example

/**
   a double precision point in two dimensional geometry, with two coordinates (x, y)
*/
public class Point
{
   public Point() { x = 0; y = 0; }
   public Point(double xx, double yy) { x = xx; y = yy; }
   public double getX() { return x; }
   public double getY() { return y; }
   private double x;
   private double y;
}

Steps to Design a Class

  1. Find out what the objects of the class should be able to do.
  2. Choose names for the methods.
  3. Document the public interface.
  4. Determine instance variables.
  5. Determine constructors.
  6. Implement methods.
  7. Test the class with a test program.

To Do After Class