What are classes and objects?
Understanding Classes and Objects are a HUGE part of Java since it's an Object-Oriented Programming language. Let's start at the bare bones:
Here are some examples of classes:
- Car
- Fruit
- Food
- Drink
- Book
Now, here are some examples of objects:
- Tesla Model 3
- Apple
- Pizza
- Coca Cola
- Diary of a Wimpy Kid
Classes are templates for objects.
Objects are instances of a class. In fact, we've already used Objects and Classes once before: Scanner
Let's look at the code below:
Scanner input = new Scanner(System.in);
Scanner is the template, our class. input, is our Scanner object, our instance of the Scanner class.
We can actually create more objects, all upon a single class.
Scanner input = new Scanner(System.in);
Scanner scan = new Scanner(System.in);
Hold up, what's Object-Oriented Programming?
In Object-Oriented Programming, we program using classes and objects. Classes contain data(also known as attributes, or details, such as color, age, etc.) and methods (which you will learn about in the next lesson, but let's think of them like actions).
These provide various benefits:
- Allows for cleaner structure in your code
- Less repetition, code once, use as much as you want
- Less code, allowing you to focus on what matters
- Improved speed and execution/compiling time
Let's go ahead and create our (sort of) first class. Create a new file called Car.java, and type the following (if not already done):
public class Car {
}
Whoa, this looks like our
Main.javafile! Yup, because that's also a class.
Quick reminder here: There is a specific chapter on Classes that will go much more in-depth than here. (I don't know why, but we gotta follow it.) We will speed through it here and understand the basics to learn more in that future chapter.
Let's create some attributes for it, and it's just like creating variables.
public class Car {
String color;
int wheels = 4;
String model;
boolean electric;
}
These are our attributes. We have one for each type of detail about our car class:
colorwheelsmodelelectric
You may also see that we didn't set the value of some variables. That's because we will give them values when we create the instance. For example, the
System.inforScannerobjects. In contrast, we did set the number of wheels.
The constructor
You will learn more about this later, but essentially, a constructor is used to feed in values to the object. This is because every instance is unique (or may not be), but may require different variables.
For example, all cars have 4 wheels. But not all cars are red. Not all cars are electric.
To create our constructor, we just do the following:
boolean electric;
public Car(String carColor, String carModel, boolean isElectric) {
color = carColor;
model = carModel;
electric = isElectric;
}
We will feed these values as parameters, which you also will learn about later. (Sorry about this, but this is how the curriculum goes.)
Then right after this, we will create our first method (we will create many more throughout this chapter.)
toString()
The toString() method is one that is built into every Object, but we can override it by creating our own. Below the constructor, write the following:
electric = isElectric;
}
public String toString() {
return("You have a " + color + " " + model +" with " + wheels +" wheels.");
}
This string prints out the color,model,andwheels.
Now, go back to Main.java, and let's create a Car object.
public static void main(String[] args) {
Car tesla = new Car("Red","Tesla Model 3",true);
}
The order of the values in the parenthesis match the same order in the constructor. color, model, then electric.
Also note that we aren't using
electricyet, we will learn about that later 😄
And finally, right below that, this:
System.out.println(tesla);
The great thing about the toString() method is that we don't need to do anything else but put the object in the String.
You should get this:
You have a Red Tesla Model 3 with 4 wheels.
Phew...
These first lessons of units are always filled with content, allowing you to understand them piece by piece as we go through.
Don't worry if you don't fully understand this lesson. It's meant to let you see the big picture before we figure out the smaller things.