Java is an object-oriented programming language that can be used to model objects in the real world. We’ve seen that Java programs start with public class
. A class is used to define a new data type or a blueprint for objects.
Objects are the variables created from a class definition, and they are instances of a class.
Java class defines what objects of the class know (attributes) and what they can do (behaviors). Each class has constructors like World()
and Turtle(habitat)
which are used to initialize the attributes in a newly created object.
A new object is created with the new
keyword followed by the class name (new Class()
). When this code executes, it creates a new object of the specified class and calls a constructor, which has the same name as the class. For example, new World()
creates and initializes a new object of the World
class, and new Turtle(habitat)
creates and initializes a new Turtle
object in the World habitat.
Let’s take the example of the a person creating a Zoo in real life. This person has a magic wand that can create objects. It can create anything he would ever want, as long as he shows the wand a blueprint of that object. The want will then create the actual physical object from the blueprint instructions. Since he is creating a zoo, maybe he wants to create physical turtles. So he makes a blueprint of these turtles that he wants. He says, they will have a color, weight and length, and they will be able to make a sound. His blueprint looks like this:
public class Turtle { //properties private String color; private int weight; private int length; private String sound; //constructor public Turtle() { color = "red"; weight = 95; length = 30; sound = "merawr"; } public Turtle(String , int someLength, int someWeight, String someSound) { color = someColor; length = someLength; weight = someWeight; sound = someSound; } }
So if a person gives the magic wand the blueprint above the Magic wand will be able to make actual turtles. This class is not a turtle itself. It’s just what a turtle COULD be.
A class is composed of Methods, properties (also called fields or instance variables) and a constructor.
We will use the constructor to make actual turtles. We can make as many turtles as we want.
Turtle jeff = new Turtle();
By using our want, we are doing what the line above does with code. It will create a basic Turtle. Notice that we didn’t put anything inside the (). That means that we want to use the () constructor. If we use that constructor our Turtle, named jeff, will have some basic properties (red, 95, 30, “merawr”).
But what if we want to make more Turtles?
Turtle jeff = new Turtle(); Turtle jeremy = new Turtle(); Turtle jeravious = new Turtle();
We can make as many turtles as we want, but if we always use the basic constructor, we will have a bunch of red turtles going around saying “merawr”.
Let’s then make a very special turtle, this time one that makes a different sound and is MUCH bigger than the other turtles that we have made.
Turtle bigBoi = new Turtle("blue", 1000, 500, "graaaawr");
This time we used the second constructor, the one that lets us define a turtle with our own chosen properties. This would now make a big turtle.
One thing that is sometime confusing is that if there is only one class, one blueprint, how can they all share the same variables. They all have their OWN variables for weight, height, sound and color. The class just shows what a Turtle COULD be.