Classes and Objects

The goal of objects is to help extend our current capabilities to store and manipulate data. In simple programming examples, primitive types are often good enough, since we are working with single data points such as integers. The typical problems we encounter in day to day programming, however, are different. These problems will typically handle data that is more complex, with many related properties that need to be worked with. Objects and classes will provide us a way to define custom data types to be able to handle any data we encounter.

A class is the programming structure we use to define all the information the program needs to know about our object. A class can be seen as the area where we describe what kind of features the object has, and how we work with the object. It sets out the rules of how an object can be used and interacted with.

Traditionally, an object will have two components related to it.

  1. Properties: Properties are things that can be used to describe the object
  2. Methods: Methods are things that interact with the object

Let’s look at a simple example of an object definition to better understand this. Suppose that you own a book shop, and you want to create a program that works with book information. To determine the properties of a book, we need to think about the information that helps identify or describe the book. For example, a title could be a property of a book, as well as the author, publisher, and length of the book. When we look at methods, we are looking at anything that can interact with the object. In our book example, this could include updating the name or publisher of a book or determining the genre of the book.

If we wanted to program our book example in Java, we would need to start by creating a class with the name Book. Once we have done this, we would next define the properties that the book has, inside of the class.

We have created three private variables inside of the class, each holding a single property of the object we described. These variables are private because we don’t want them to be accessed directly from outside of the object. Instead, we will create methods that allow us to get and set the values of these properties when required. The reason that we do this is because it allows us to validate the data before we use it. If a user tries to set one of the properties to something invalid, we can catch the issue and handle it, rather than allowing it and failing later.

Methods are defined similarly to functions, we give them a name and a return type, and allow them to be public so they can be accessed outside of the class definition. When we reference properties inside of the methods, we use the this keyword, to indicate we are talking about a property of the object. For our properties, we want to provide the user with a way of getting the values of the properties, as well as setting the values of the properties. We refer to these types of methods as setters and getters, or sometimes accessors and mutators. In addition to the getter and setter methods, there is one other special type of method called a constructor that we want to make. A constructor is called when the object is first created in our program, and determines what values the properties should be set to when the object is first created. This allows us to be able to initialize our object, rather than leaving properties as undefined or uncertain values.

Our constructor is the first method below the property definitions. Notice that this method doesn’t require a return type or void, it just needs public, and the name of the class. With the current constructor, we allow the user to input values for the book name, publisher, and book length, and we set the properties based on what is provided. In addition to the constructor, we have also created three getter methods, which return the values of the properties they are named after.

We can define setter properties to set the values of the properties in a similar way to the getters. The only difference is that they take in an argument to change the value, and typically don’t return anything once completed.

The main takeaways from this class is that we set up properties as values that describe the object using private variables. When we want to set or get the values of these properties, we utilize getter and setter methods, which are public, and allow us to validate the values before setting the properties.

Now that we have a class defined, the final step is to see how it can be used in Java as an object. To declare an object in Java, we use syntax that is like declaring a variable. The only difference is that we call the constructor at the time of declaring the variable, so we need to provide initial values for the book name, publisher, and length of the book.

For our definition, the left-hand side of the assignment is the same as any other variable declaration. We are giving a variable named test a type that is Book. The right-hand side of the assignment allows us to create an initial value for the test variable. We do this by telling Java to call the constructor using the keywords new Book. Inside of the brackets, we provide the three arguments that initialize the properties we defined in the class. Once this is done, Java will have created an object with the three properties specified. From here, we can continue to call methods to practice manipulating and accessing properties. We do this using the syntax test.MethodName().

If the process of calling methods looks familiar, it is because you have likely used objects in Java at some point. Variables such as Strings, Arrays, ArrayLists, and Collections are all objects written for Java. Since objects are used so frequently, understanding how they are written and implemented will help you write more effective code.

One Reply to “Classes and Objects”

Leave a Reply

Your email address will not be published. Required fields are marked *