Please enable JavaScript to use CodeHS

Chapter 2

Using Objects

2.1 Objects: Instances of Classes

Overview

Classes and Objects are utilized in Java as part of the object-oriented programming model. This model focuses on objects and the data and actions associated with the objects.

Objects are variables of user-defined data types. You can refer to these as reference type variables. Just as you initialize and declare primitive types, reference types are also declared and initialized. The initialized form of a reference type is referred to as an object.

Objects

Objects are structures that contain a state and behavior. Everyday objects you commonly use have states and behaviors. For example, a car is an object with both a state and behavior.

State

The state contains information about the specific object. An example of a state for a car would be how much fuel it has.

Behavior
The behavior is the actions that can be performed on a specific object. A behavior of the car may be to get the mileage from the remaining fuel.

Classes

Classes are the templates you use for creating objects.

Here is an example of a class that lets us create a rectangle, and get its area:

public class Rectangle
{
  private double width;
  private double height;

  public Rectangle(double rectWidth, double rectHeight)
  {
    width = rectWidth;
    height = rectHeight;
  }

  public int getWidth()
  {
    return width;
  }

  public int getHeight()
  {
    return height;
  }

  public int getArea()
  {
    return width * height;
  }

  public String toString()
  {
    String rectInfo = "Rectangle with width " + width + " and height " + height + 
     " and area " + getArea();

    return rectInfo;
  }
}
Plain text

Here is another example of a class that lets us create new animal objects:

public class Animal
{
  private String name;
  private boolean isPet;
  private int age;

  public Animal(String animalName, boolean isAnimalPet, int animalAge)
  {
    name = animalName;
    isPet = isAnimalPet;
    age = animalAge;
  }

  public String getName()
  {
    return name;
  }

  public boolean getPetStatus()
  {
    return isPet;
  }

  public int getAge()
  {
    return age;
  }

  public String toString()
  {
    String aInfo = "This animal's name is " + name + ". They are currently a pet: " +
      isPet + ". The animal is " + age + " years old.";

    return aInfo;
  }
}
Plain text

Here is another example of a class that takes in a vehicle type, its age, and how many miles it has:

public class Vehicle
{
  private String vehicleType;
  private int vehicleAge;
  private double vehicleMiles;

  public Vehicle(String vType, int vAge, double vMiles)
  {
    vehicleType = vType;
    vehicleAge = vAge;
    vehicleMiles = vMiles;
  }

  public String getType()
  {
    return vehicleType;
  }

  public int getAge()
  {
    return vehicleAge;
  }

  public double getMiles()
  {
    return vehicleMiles;
  }

  public double estimateMilesPerYear()
  {
    return vehicleMiles / vehicleAge;
  }
}
Plain text

Classes vs Objects

It is important to remember that classes are just templates for creating new objects. A class is a formal implementation, or blueprint, of the attributes and behaviors of an object.

An object on the other hand is a specific instance of a class with defined attributes. Objects contain both a state and behavior, and are an instance of a class.

Instances

An instance is a specific version of an object that can differ in numerous ways. Going back to the previous section, Rectangle, Animal, and Vehicle are all classes. If you create two different vehicles they would be specific instances of the Vehicle class. Or in other words, they would be two different vehicle objects. Remember, an object is an instance of a class.

Examples

public class ExampleClass
{
 public static void main(String[] args)
 {
  // `Animal` is our class.

  // `myDog` and `myCat` are objects, because
  // they are specific instances of our `Animal` class.
  Animal myDog = new Animal("Cujo", true, 7);
  Animal myCat = new Animal("Kerby", true, 2);
 }
}
Plain text
public class ExampleClass
{
 public static void main(String[] args)
 {
   // `Rectangle` is our class.

   //`mySquare` and `myRectangle` are objects, because
   // they are specific instances of our `Rectangle` class.
   Rectangle mySquare = new Rectangle(20, 20);
   Rectangle myRectangle = new Rectangle(5, 10);
 }
}
Plain text

Rectangle Skeleton

Here is an example of the first piece of a class definition. This code will be used later to create objects. Until we use the class definition to instantiate objects, this code will not do anything.

Hence, this code is for you to look at and not to run. Nothing will happen if you hit run.

GrilledCheese Skeleton

Here is an example of the first piece of a class definition. This code will be used later to create objects. Until we use the class definition to instantiate objects, this code will not do anything.

Hence, this code is for you to look at and not to run. Nothing will happen if you hit run.

Shark Skeleton

Here is an example of the first piece of a class definition. This code will be used later to create objects. Until we use the class definition to instantiate objects, this code will not do anything.

Hence, this code is for you to look at and not to run. Nothing will happen if you hit run.

Check Your Understanding

  1. Incorrect Correct No Answer was selected Invalid Answer

    Consider this class definition of Weather.

    public class Weather
    {
        private boolean isRaining;
        private String forecast;
        private double temperature;
    
        // Rest of class goes here
    }
    Java

    When we use this class to create Weather objects, which of the following is guaranteed to be true?

Exercise: Ice Cream Instance Variables

This class represents IceCream.

Add the instance variables, or attributes, that every ice cream object should have to the IceCream.java file. Remember to give the variables a type and a name. They should not have values at this point.