Subtitles section Play video Print subtitles (upbeat electronic music) - [Kathryn] Almost anything in real life can be represented in code, but how well something is represented is up to us in what we decide to program. Classes in Java give us a way to model or represent physical objects in code via a blueprint. A blueprint or class contains a set of attributes and behaviors that define an object. Let's design a tree object in code using a class. The attributes might be height, trunk diameter, and maybe even tree type. Every tree has these properties or attributes, but their values might not be the same. That's what makes this class a blueprint. It contains the definition of what a class should be. As for the behaviors, growing could be a behavior for the tree. It's an action that the tree takes rather than an attribute representing the current state of the tree. Let's create a blueprint for a tree using a class in Java. I'm on a Mac, so I'll go control click, and this will allow me to create a new Java class. We'll call this "tree," and our IDE sets up this class as an empty class named "tree". A class doesn't have to have attributes or behaviors. It could be completely empty, as we see here. It could also just have attributes or just have behaviors. However, most of the time a given class will have both. Let's add some code to the "tree" class so that it actually represents a tree. Starting with the attributes, we'll have a height and trunk diameter. These will have the data type double, meaning they can be represented by decimal values. While we could represent the type of tree with a string, we'll represent it with its own enum here called "tree type." To create an enum, we'll control click on the source folder, select new, Java class, and then enum. We'll call this enum "tree type." An enum is a special type that represents a group of constants. Here, we'll want those constants to be different tree types. These are the only values we can give to a variable with the tree data type. By using an enum, no one can create a macaroni tree or a money tree, which would be the case if we used a string. The only tree types that can exist are those listed in the enum class. To use this enum, all we have to do is use the data type "tree type" for a given attribute or variable, which is what we've done here. Now our class has attributes. Let's add the grow behavior to finish up our definition of a tree. A behavior is represented by a function. Our function will return void and be called grow. In the implementation, we'll access the height and trunk diameter of the tree, and increment them accordingly. To keep things simple, we'll say all trees will grow 10 feet in height and one inch in diameter when this behavior is used. However, you could use the type attribute or other data to inform how much the tree grows. There are many different ways a tree can be represented in code. Great, now we've combined the attributes and behaviors related to a tree into a single unit: the "tree" class. Remember, it's a blueprint, because we're only defining the attributes and behaviors of a tree. We haven't created any trees yet. Our class says if we wanted to create a tree, this is how it would be represented in our program. Classes only represent a general blueprint, but they become more tangible when you use a constructor to bring your blueprint to life. To create trees from our "tree" class, we'll need to add a special type of function called a constructor, to construct our tree objects. To create a constructor, we'll use the class name "tree." We don't need a return type because the name of the function "tree" counts as the return type as well, since "tree" is the class name. This classifies it as a constructor. To build a tree, we'll need to know what its height, diameter, and tree type should be. This means we'll need to add inputs to the constructor so that we can construct a custom tree with the appropriate height, diameter, and type. Of course, the body of the function doesn't do anything yet. We'll need to set up the tree we're building with the appropriate values. Using the "this" keyword, we access the tree we're in the midst of creating and set the height, trunk, and type on the left equal to the inputted values on the right. When this constructor is used, it will create a tree with the inputted height, trunk, and tree type values. Let's try it. To keep our "tree" class separate from the program we want to execute, we'll create a new class called "main." This will contain our main method where we'll execute the program from. To create a tree, we'll use the constructor "tree" with the inputs 25 for the height, five for the diameter, and our tree will be an oak tree. In addition to using the name of the constructor, we'll also need to add the new keyword. This will create a new tree from our "tree" class. So right now we create the tree, but then we throw it away. We don't save it in a variable anywhere. To save it, we'll create a tree variable called "my favorite oak tree." We've created our first tree. If we write "my favorite oak tree," we'll be able to see all of the attributes as well as the behaviors we can use on the tree. To start off, we'll just print out the tree's type. We'll use system.out.printLN and run the program. And it's an oak tree! By adding a constructor to our "tree" class and using the constructor in a main method, we were able to create our first tree in code. (mellow electronic music)
B1 tree class diameter blueprint height represented Java Tutorial - Creating objects with constructors 22 1 Summer posted on 2022/11/27 More Share Save Report Video vocabulary