Skip to content

Creating Classes

  • Lesson
  • 1 min read
  • 3 of 5 in this chapter

Classes, again?

We've already covered the basics of classes, and this lesson will be a quick refresher. Throughout this chapter, we will be creating a Vehicle class. (We will be building upon this in future chapters as well)

We will give each Vehicle object different colors, model types, features, and more.

We will also create methods that we can use to learn more about the vehicle and print out the car's variables.

Creating the Class

First, start by making a new file (if you haven't already) named Vehicle.java.

Then, let's get the file set up with some variables and a general Class structure:

Java
public class Vehicle {
  private String type;
  private String color;
  private String model;

  private boolean isElectric;

  private int wheels = 4;
  private int cost;
}

These variables are called attributes, which are essential "details" of the Object.

We make all of our attributes private, as we will be making setter and getter methods soon.

The only value we set is wheels, but we will add an option to change them if necessary.

In the next lesson, we will create various constructors, allowing us to set values of the object attributes.