Join Peggy Fisher for an in-depth discussion in this video Creating classes, part of Learning Java 8.
- Classes are a critical component of all object-oriented programming languages. The class is a blueprint for an object. The classes are used to represent the state and behavior of real world objects, such as person, bank account, vehicle, et cetera. All classes contain instance data, constructors, which are special methods used to build an instance of our class, and methods, the blocks of code common to all objects of this class that represent the object's behavior.
You might be asking, why classes? Well, classes are reusable. Multiple programs can use the same class. Classes also allow us to encapsulate data. That means to protect the data, to help enforce data integrity. Data is defined as private inside the class. Data is then accessed using get and set methods, which are otherwise known as accessor and mutator methods. I think it's time for an example.
I've started a new project called RealEstateListings, but before we start coding our main method, we need to add a separate class file. In this case, I'm going to add a property class. Make sure that the class file is added to the same package folder as our main. I'm going to click on RealEstateListings. I'm going to click on the New File button. Here you'll notice on the left, category is Java. On the right, the file type at the top is Java Class.
I'll click next. I'm going to call this Property, and I'll click finish. The first thing we need to do in the property class is to define our instance data. Notice, it does not have a main method. The class does not contain a main. I'm going to start by adding a few fields that we need for instance data. Next we define the constructor or constructors. Constructors are easy to identify. They have no return type, and they have the exact name as the class. In our example, we're going to have two constructors, one for a property with a structure and one for land.
The first one is the constructor for land only, so there is no number of baths and number of beds. I use the constructor to put values into my instance data. I use the keyword this.askingPrice to indicate that I'm referring to the instance data and not the value that's coming in as a parameter, since they have the same name. I'll do this for the other two pieces of data. I want to make sure that the numBaths and numBeds have a value. I'll set them both to zero.
Next, I'm going to create a second constructor using method overloading. This time, I'll have all five values. My two constructors are done. The last thing I want to do is to add a method that will allow me to return a string that prints the information nicely. Okay, now I have my class created. In the next section, we will use this class to create multiple property objects.
One last thing I want to mention. Classes can be represented visually using a UML diagram. If you would like more practice in creating UML diagrams, please refer to Foundations of Programming: Object-Oriented Design, Introduction to the Unified Modeling Language by Simon Allardice.
- Downloading and exploring NetBeans
- Understanding Java basics: data types, strings, arrays, and more
- Controlling flow with functions and loops
- Creating classes
- Sorting and searching arrays
- Manipulating files
- Handling errors
- Building GUIs