Basic Java Object Oriented Programming

Java was written around the concept of Object Oriented Programming. That’s OOP for short. To understand Java programming, it’s useful to know what is meant by “objects” and “object oriented.”

Let’s explore objects by using the example of a car. A car has an owner, gas, and the ability to move. A car is an object. Objects encapsulate identity, state, and behavior. In the case of our car, the car’s identity might be Tom’s car. It is in the state of having a half tank of gas. Also, one of the car’s behaviors is its ability to accelerated so it is moving or braking to a stop.

Here is a representation of car written in Java.

package com.genedavis;

/**
 * This Car class is the template for all
 * car objects that we create.
 */
public class Car {
  /**
   * The state of the car's movement.
   */
  boolean moving = false;
	
  /**
   * The amount of gas in the tank.
   */
  int gallonsOfGas = 0;
	
  /**
   * The car owner's name.
   */
  String owner = "";
	
	
  /**
   * Is the car moving?
   * 
   * @return true if the car is moving
   */
  public boolean isMoving() {
    return moving;
  }
	
  /**
   * Take the car from a stopped state
   * to a moving state.
   */
  public void accelerate() {
    this.moving = true;
  }
	
  /**
   * Take the car from a moving state
   * to a stopped state.
   */
  public void brake() {
    this.moving = false;
  }
	
  /**
   * Check the gauge for the number
   * of gallons of gas in the car.
   * 
   * @return number of gallons of gas in the tank.
   */
  public int viewGauge() {
    return gallonsOfGas;
  }
	
  /**
   * Fill the gas tank.
   * 
   * @param gallonsOfGas
   */
  public void addGas(int gallonsOfGas) {
    this.gallonsOfGas += gallonsOfGas;
  }
	
  /**
   * Retrieve the car owner's name.
   * 
   * @return The owner's name.
   */
  public String getOwner() {
    return owner;
  }
	
  /**
   * Change the owner's name.
   * 
   * @param owner
   */
  public void setOwner(String owner) {
    this.owner = owner;
  }
}

 

The above code is a Car class. It is stored in a file named Car.java and compiled down to Car.class. Think of classes as templates or blueprints for objects. First you design the object in a class, and then you use the object in a real program, just like in the real world, an engineer would first make a blueprint for a car and then make lots and lots of cars from the blueprint.

Here is an example of code using the Car class to create an instance of a car.

package com.genedavis;

public class Main {

  /**
   * Everything starts here.
   */
  public static void main(String[] args) {
		
    //create a car from the Car class
    Car tomsCar = new Car();
    
    //selling the car for the first time
    tomsCar.setOwner( "Tom" );
		
    // filling the tank
    tomsCar.addGas( 12 );

    // taking it for a spin
    tomsCar.accelerate();
		
    // trying to avoid crash
    tomsCar.brake();
		
    // selling the car to unsuspecting buyer
    tomsCar.setOwner( "Bob" );
		
  }
}

 

Organizing the objects in many applications is easy when the application is representing or manipulating real world objects or graphic interfaces. It’s easy to figure out the objects for a calculator for instance. You need a Button class that the number button objects are created from, and a Display class that allows a display object to handle displaying answers and numbers that were typed in. Object Oriented Programming makes organizing actions and attributes easy when a real world thing is being described.

Where OOP gets a bit sketchy is when the system being designed is conceptual, rather than something that actually exists. Imagine you are designing a program to solve word searches. The concepts involved can be organized in many ways, but mostly bad ways.

The first task you have when designing a new Object Oriented Program is to create a list of attributes and actions that your program is responsible for. Separate related actions and attributes and clump them together. If one clump of actions and attributes is two big, split that group into multiple subgroups of actions  and attributes. When you have your actions and attributes broken down into manageable chunks, name them. You now have a the classes for your application.

Summary

This introduction works for small applications, but is only a very cursory description. For larger applications, the types of applications that cost tens or hundreds of millions of dollars to create, we often see whole teams using specialized methodologies working on the application’s design. However, this OOP introduction is good enough for the beginning Java programmer.