Home CPSC 330

Object-Oriented Basics

Goals of Object-Oriented Programming


Classes & Objects

Objects combine data members and functions together in one unit. An object consists of three things:

Objects are defined as being part of some class. A class is a blueprint for an object and describes what attributes and behavior it will have. An object is an "instance" of class. There can be many objects from the same class.


Static Methods

We have seen that the main method in Java must be "static".

A static method is one where there is no object of the class created.

Another example of static methods is found in the Java Math class. In this example, we call several static methods of Math.


import java.util.Scanner;

public class MathExample {
  public static void main(String args[]) {
    // create a Scanner object
    Scanner in = new Scanner(System.in);
    
    // prompt for input
    System.out.println("Enter a number: ");
    
    // read in an int
    double num = in.nextDouble();

    // do math on it
    System.out.printf("Square root: %f, Sin: %f, Abs: %f\n",
        Math.sqrt(num), Math.sin(num), Math.abs(num));
  }
}

A static method does not have an object to work on. This means it cannot access the member variables, or methods. In this example, we try to access non-static properties from main.


public class Static1 {
  private int x = 5;

  public void printX() {
    System.out.printf("X = %d.\n", x);
  }

  public static void main(String args[]) {
    printX();
  }
}

This does not work. Each object has its own copy of x. So a static method such as main cannot access them since we have no Static1 object at all.

We can access static properties, however:


public class Static2 {
  private static int x = 5;

  public static void printX() {
    System.out.printf("X = %d.\n", x);
  }

  public static void main(String args[]) {
    printX();
  }
}

Static methods are used in situations where the class simply groups related methods. They are also used when a class has some method that logically is part of a class, but doesn't need an object.

They are also used as the basis for a few design patterns we'll see later.


Encapsulation

As in C++, each member of a class in Java can be private or public.

Unlike C++, each member must explicitly be marked private or public. There is no "public section" and "private section".

In general, all class data should be private. There are several reasons for this:

A class that does not expose any more information than necessary is said to be well encapsulated.

Encapsulated classes are easier to use, test, debug and maintain.

In the following example, we create a class for storing a contact list. We use an array for this, and make the array public.

By exposing the array as public data, we are not encapsulating this class very well. This would cause problems if:

This version fixes this by encapsulating the array.


Finding the Objects

A large part of object-oriented design is in deciding what classes to create, and how they interact with each other.

A good rule of thumb is to think about the nouns in the program description. These can form the objects in the program. Then think about the actions that those nouns perform, these can form the methods.

We will discuss techniques for designing object-oriented programs throughout the semester.

Copyright © 2018 Ian Finlayson | Licensed under a Creative Commons Attribution 4.0 International License.